hdu 1068 Girls and Boys 二分圖的最大匹配

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1068


#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int n;
int used[505];
int link[505][505];
int boy[505];

int find(int x){
    int i;
    for(i=0;i<n;i++){
        if(!used[i]&&link[x][i]){
            used[i]=1;
            if(boy[i]==0||find(boy[i])){
                boy[i]=x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int all;
    int t,per,i,num;
    while(scanf("%d",&n)!=EOF){
        memset(link,0,sizeof(link));
        memset(boy,0,sizeof(boy));
        all=0;
        for(i=0;i<n;i++){
            scanf("%d: (%d)",&t,&num);
            while(num--){
                scanf("%d",&per);
                link[t][per]=1;
            }
        }
        for(i=0;i<n;i++){
            memset(used,0,sizeof(used));
            all+=find(i);
        }
        printf("%d\n",n-all/2);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章