hdu 1281 棋盘游戏

/*
一看就是最大匹配然后删边....
开始不会建图...总觉得有好多 分 ... 
看了看题解
二分图嘛 按行列建图 
某个点能放车 x连y
最后删掉这个匹配和边 看看能不能匹配 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 110
using namespace std;
int n,m,k,G[maxn][maxn],match[maxn],mx,ans,cas;
bool f[maxn];
void Clear(){
    ans=mx=0;memset(G,0,sizeof(G));
    memset(match,0,sizeof(match));
}
int Dfs(int u){
    for(int v=1;v<=m;v++)
        if(G[u][v]&&f[v]==0){
            f[v]=1;
            if(match[v]==0||Dfs(match[v])){
                match[v]=u;return 1;
            }
        }
    return 0;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k)){
        Clear();int u,v;
        for(int i=1;i<=k;i++){
            scanf("%d%d",&u,&v);
            G[u][v]=1;
        }
        for(int i=1;i<=n;i++){
            memset(f,0,sizeof(f));
            mx+=Dfs(i);
        }    
        for(int i=1;i<=n;i++){
            memset(f,0,sizeof(f));
            int x=match[i];
            G[x][i]=0;match[i]=0;
            if(!Dfs(x)){
                ans++;match[i]=x;//这个放if里面 
            }
            G[x][i]=1;
        }
        printf("Board %d have %d important blanks for %d chessmen.
",++cas,ans,mx);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yanlifneg/p/5965417.html