--hdu1281->二分图最大匹配(唯一匹配判断)

D - 棋盘游戏
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

小希和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.
 
先找出一个最大匹配,然后将每个匹配去掉,再查找最大二分匹配,如果不是必须的,则去掉也不影响最大匹配的数量。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int ms[101][101],match[101],vis[101];
int n,m;

int hungray(int u){

    for(int i = 1;i<=m;i++){
        if(ms[u][i] && vis[i] == 0){
            vis[i] = 1;
            if(match[i] == 0||hungray(match[i])){
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}

int main(){

    int k;
    int c = 1;
    while(~scanf("%d%d%d",&n,&m,&k)){

        int ans,res;
        ans = res  = 0;

        memset(ms,0,sizeof(ms));
        memset(match,0,sizeof(match));

        int x,y;

        for(int i = 0;i<k;i++){
            scanf("%d%d",&x,&y);
            ms[x][y] =  1;
        }

        for(int i  = 1;i<= n;i++){
            memset(vis,0,sizeof(vis));
            if(hungray(i))ans ++;
        }
        int match1[101];

        for(int i = 1;i<=m;i++)match1[i] = match[i];

        for(int i = 1;i<= m;i++){
            memset(match,0,sizeof(match));
            int ans1 = 0;
            if(match1[i]){
              //  printf("匹配为:%d %d\n",match[i],i);

                ms[match1[i]][i] = 0;

                for(int j = 1;j<=n;j++){
                    memset(vis,0,sizeof(vis));
                    if(hungray(j))ans1++;
                }
                if(ans1 < ans)res++;
                ms[match1[i]][i] = 1;
            }
        }
        //printf("%d %d\n",res,ans);
        printf("Board %d have %d important blanks for %d chessmen.\n",c++,res,ans);

    }

}

  

 
posted @ 2015-08-04 10:23  袁汉春  阅读(744)  评论(0编辑  收藏  举报