UVa 1663 - Purifying Machine(二分匹配)

给出一些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;
}

你可能感兴趣的:(uva,二分匹配)