【题目链接】 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;
}

最新文章

  1. C++ 最小化到托盘
  2. Spring AOP支持的AspectJ切入点语法大全
  3. 【Windows编程】系列第四篇:使用Unicode编程
  4. 1089 最长回文子串 V2(Manacher算法)
  5. 关于DOM的一些操作 整理 积累
  6. TL-WR702N 连接有线路由
  7. Android 获取屏幕高度,宽度,状态栏高度
  8. 函数buf_pool_get
  9. FAILURE: Build failed with an exception. Crunching Cruncher screen.png failed
  10. 一个好看的Input样式
  11. div大小如何改变设置
  12. centos7 jsoup java.net.UnknownHostException
  13. MessageDigest简要
  14. JS组件系列——自己动手封装bootstrap-treegrid组件
  15. 有史以来功能最全,使用最简单的excel导入/导出工具
  16. Dojo API中文 Dojo内容模块概览,初学者
  17. response 输出中文数据 文件下载
  18. scrapy之持久化存储
  19. 使用pyinstaller打包python小程序(没有使用第三方模块)
  20. java系列之 原生数据类型

热门文章

  1. Oracle 学习----:ora-00054 资源正忙 ,但指定以nowait方式获取资源 ,或者超时失效---解决方法
  2. [转]unity之LOD
  3. 3D U-Net卷积神经网络
  4. 爬虫:Scrapy7 - Scrapy终端(Scrapy shell)
  5. 团队项目-第九次scrum 会议
  6. python 读取consul配置
  7. Java获取当前服务器IP实现
  8. Redis键管理
  9. 利用反射修改final数据域
  10. HDU 1054树形DP入门