UVa 1663 - Purifying Machine(二分匹配)

给出一些01串,含星号的串表示包含两个串,星号位置分别为0和1。
每次可以消掉一个串或者两个只有一个数字不同的串,求最少几次可以消掉所有串。
读出所有串,两两判断能否一起消掉,然后其最大匹配数即可。具体细节见代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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;
}

** 本文迁移自我的CSDN博客,格式可能有所偏差。 **