POJ 3041 Asteroids (二分图匹配)
2024-10-20 05:38:14
【题目链接】 http://poj.org/problem?id=3041
【题目大意】
一个棋盘上放着一些棋子
每次操作可以拿走一行上所有的棋子或者一列上所有的棋子
问几次操作可以拿完所有的棋子
【题解】
每个棋子相当于是连接行列二分图的边,我们做一遍二分图匹配就是答案。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int MAX_V=1000;
int V,match[MAX_V];
vector<int> G[MAX_V];
bool used[MAX_V];
void add_edge(int u,int v){
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v){
used[v]=1;
for(int i=0;i<G[v].size();i++){
int u=G[v][i],w=match[u];
if(w<0||!used[w]&&dfs(w)){
match[v]=u;
match[u]=v;
return 1;
}
}return 0;
}
int bipartite_matching(){
int res=0;
memset(match,-1,sizeof(match));
for(int v=0;v<V;v++){
if(match[v]<0){
memset(used,0,sizeof(used));
if(dfs(v))res++;
}
}return res;
}
const int MAX_K=10000;
int N,K;
int R[MAX_K],C[MAX_K];
void solve(){
V=N*2;
for(int i=0;i<K;i++)add_edge(R[i]-1,N+C[i]-1);
printf("%d\n",bipartite_matching());
}
void init(){
scanf("%d",&K);
for(int i=0;i<K;i++)scanf("%d %d",&R[i],&C[i]);
}
int main(){
while(~scanf("%d",&N)){
init();
solve();
}return 0;
}
最新文章
- C++ 最小化到托盘
- Spring AOP支持的AspectJ切入点语法大全
- 【Windows编程】系列第四篇:使用Unicode编程
- 1089 最长回文子串 V2(Manacher算法)
- 关于DOM的一些操作 整理 积累
- TL-WR702N 连接有线路由
- Android 获取屏幕高度,宽度,状态栏高度
- 函数buf_pool_get
- FAILURE: Build failed with an exception. Crunching Cruncher screen.png failed
- 一个好看的Input样式
- div大小如何改变设置
- centos7 jsoup java.net.UnknownHostException
- MessageDigest简要
- JS组件系列——自己动手封装bootstrap-treegrid组件
- 有史以来功能最全,使用最简单的excel导入/导出工具
- Dojo API中文 Dojo内容模块概览,初学者
- response 输出中文数据 文件下载
- scrapy之持久化存储
- 使用pyinstaller打包python小程序(没有使用第三方模块)
- java系列之 原生数据类型