UVa 1663 - Purifying Machine(二分匹配)
2015-08-29 20:59
253 查看
给出一些01串,含星号的串表示包含两个串,星号位置分别为0和1。
每次可以消掉一个串或者两个只有一个数字不同的串,求最少几次可以消掉所有串。
读出所有串,两两判断能否一起消掉,然后其最大匹配数即可。具体细节见代码。
每次可以消掉一个串或者两个只有一个数字不同的串,求最少几次可以消掉所有串。
读出所有串,两两判断能否一起消掉,然后其最大匹配数即可。具体细节见代码。
#include<cstdio> #include<cstring> using namespace std; const int maxn=2100; int n,m; char s[15]; int a[maxn]; bool _set[maxn],g[maxn][maxn]; int from[maxn]; bool vis[maxn]; bool match(int x){ for(int i=0;i<maxn;++i) if(g[x][i]&&!vis[i]){ vis[i]=true; if(from[i]==-1||match(from[i])){ from[i]=x; return true; } } return false; } int hungary(){ int tot=0; memset(from,-1,sizeof from); for(int i=0;i<maxn;++i){ memset(vis,0,sizeof vis); tot+=match(i); } return tot; } int main(){ while(~scanf("%d%d",&n,&m)&&(n||m)){ memset(g,0,sizeof g); memset(_set,0,sizeof _set); for(int i=0;i<m;++i){ scanf("%s",s); int pos=-1,tmp=0; for(int j=0;j<n;++j) if(s[j]=='1') tmp|=1<<j; else if(s[j]=='*') pos=j; _set[tmp]=true; if(pos!=-1){ tmp|=1<<pos; _set[tmp]=true; } } m=0; for(int i=0;i<maxn;++i) if(_set[i]){ ++m; for(int j=0;j<n;++j){ int tmp=i^(1<<j); if(_set[tmp]) g[i][tmp]=true; } } printf("%d\n",m-hungary()/2); } return 0; }
相关文章推荐
- String和StringBuffer
- poll&&epoll实现分析(一)—poll实现
- iOS开发之有趣的UI —— 数据转模型及MVC设计模式
- 键盘输入数组,然后输出最大
- Chef and Prime Divisors(GCD)
- HDU2.2.5 三角形
- Java面试题笔试题:
- ReactiveCocoa信号使用方法
- CFileDialog 打开目录文件 保存目录文件
- django分页技术paginator
- python_学习笔记0829
- c3p0连接池连接MySql数据库时,无请求自动断开连接的解决方案
- 基础多线程
- poj 1823 Hotel(线段树·区间更新·lazy tag)
- 平方数的口算或巧算
- Android UI 绘制过程浅析(五)自定义View
- Cocoapods安装AVOSCloudIM出现错误
- 服务器编程入门(7)I/O复用
- Introduction to WPF Templates(WPF模板简介)
- Objective-C 【单个对象内存管理(野指针&内存泄露)】