POJ 1274题

//题目类型:二分图的最大匹配
//基础的二分匹配题:使用匈牙利算法实现,可以作为模板
//注:使用scanf,prinf输入输出否则会超时
#include <stdio.h>
#include <string.h>
//#include <conio.h>
#define arraysize 201
int map[arraysize][arraysize];
int match[arraysize];
bool final[arraysize];
int n,m;
bool DFS(int p)
{
     int i,j;
     int temp;
     for(i=1;i<m+1;++i)
     {
        if(map[p][i] && !final[i])
        {
            final[i] = true;
            temp = match[i];
            match[i] = p;
            if(temp==0 || DFS(temp)) return true;
            match[i] = temp;
        }
     }
     return false;
}
int mat()
{
    int i,j;
    int maxmatch = 0;
    for(i=1;i<n+1;++i)
    {
        memset(final,0,sizeof(final));
        if(DFS(i)) maxmatch++;    
    }
    return maxmatch;
}
int main()
{
    //freopen("1.txt","r",stdin);
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
       memset(map,0,sizeof(map));
       memset(match,0,sizeof(match));
       for(i=1;i<n+1;++i)
       {
           int tempcount;
           //cin>>tempcount;
           scanf("%d",&tempcount);
           int milk;
           for(j=0;j<tempcount;++j)
           {
               //cin>>milk;
               scanf("%d",&milk);
               map[i][milk] = 1;
           }
       }
       //cout<<mat()<<endl;
       printf("%d\n",mat());
    }
    //getch();
    return 0;
}

你可能感兴趣的:(poj)