您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: