Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8829 | Accepted: 4104 |
Description
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
Input
Output
Sample Input
5 5 2 2 5 3 2 3 4 2 1 5 3 1 2 5 1 2
Sample Output
4
Source
#include<stdio.h>
#include<string.h>
bool mat[201][201],used[201];
int link[201],n,m;
bool can(int x)
{
for(int i=1;i<=m;i++)
if(used[i]==false&&mat[x][i])
{
used[i]=true;
if(link[i]==-1||can(link[i]))
{
link[i]=x;
return true;
}
}
return false;
}
int MaxMatch()
{
int num=0;
memset(link,-1,sizeof(link));
for(int i=1;i<=n;i++)
{
memset(used,false,sizeof(used));
if(can(i)) num++;
}
return num;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(mat,false,sizeof(mat));
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
for(int j=1;j<=k;j++)
{
int y;
scanf("%d",&y);
mat[i][y]=true;
}
}
printf("%d/n",MaxMatch());
}
return 0;
}