hdu1281二分图匹配

小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。 
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么? 

Input输入包含多组数据, 
第一行有三个数N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盘的高、宽,以及可以放“车”的格子数目。接下来的K行描述了所有格子的信息:每行两个数X和Y,表示了这个格子在棋盘中的位置。 
Output对输入的每组数据,按照如下格式输出: 
Board T have C important blanks for L chessmen. 
Sample Input

3 3 4
1 2
1 3
2 1
2 2
3 3 4
1 2
1 3
2 1
3 2

Sample Output

Board 1 have 0 important blanks for 2 chessmen.
Board 2 have 3 important blanks for 3 chessmen.
题意:中文很好懂,放车不能相互攻击,就是重要点,是指删除该条边后不能形成最大匹配的点
题解:很巧妙的二分图匹配,将x与y匹配,二重循环删边,判断重要点,时间复杂度大概是O(n*m);
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const double eps=1e-8;
const int N=105,maxn=305,inf=0x3f3f3f3f;

int n,m,k,color[N];
bool ok[N][N],used[N];

bool match(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(ok[x][i]&&!used[i])
        {
            used[i]=1;
            if(color[i]==0||match(color[i]))
            {
                color[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int solve()
{
    int ans=0;
    memset(color,0,sizeof(color));
    for(int i=1;i<=n;i++)
    {
        memset(used,0,sizeof(used));
        if(match(i))ans++;
    }
    return ans;
}
int main()
{
    int num=0;
    while(cin>>n>>m>>k){
        memset(ok,0,sizeof(ok));
        while(k--){
            int x,y;
            cin>>x>>y;
            ok[x][y]=1;
        }
        int ans=solve(),res=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(ok[i][j])
                {
                    ok[i][j]=0;
                    if(solve()!=ans)res++;
                    ok[i][j]=1;
                }
            }
        }
        cout<<"Board "<<++num<<" have "<<res<<
        " important blanks for "<<ans<<" chessmen."<<endl;
    }
    return 0;
}

 

posted @ 2017-04-19 11:45  walfy  阅读(128)  评论(0编辑  收藏  举报