POJ 1274 The Perfect Stall(二分图匹配/匈牙利算法)

题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27500#problem/A

代码:

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

using namespace std;

int n,m;
int maps[205][205];
int book[205];
int mach[205];

int judge(int x)
{
    for(int i=1;i<=m;i++)
    {
        if(maps[x][i]==1&&book[i]==0)
        {
            book[i]=1;
            if(mach[i]==0||judge(mach[i]))
            {
                mach[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(maps,0,sizeof(maps));
        memset(mach,0,sizeof(mach));

        for(int i=1;i<=n;i++)
        {
            int a,x;
            scanf("%d",&a);
            for(int j=0;j<a;j++)
            {
                scanf("%d",&x);
                maps[i][x]=1;
            }
        }

        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(book,0,sizeof(book));
            if(judge(i))
                ans++;
        }
        printf("%d\n",ans);
    }
}


你可能感兴趣的:(POJ 1274 The Perfect Stall(二分图匹配/匈牙利算法))