Girls and Boys
Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 3
The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:
the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)
The student_identifier is an integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int map[1100][1100];
int visit[1100];
int f[1100];
int n;
int dfs(int cur)
{
int i;
for(i=1;i<=n;i++)
{
if(!visit[i]&&map[i][cur])
{
visit[i]=1;
if(!f[i]||dfs(f[i]))
{
f[i]=cur;
return 1;
}
}
}
return 0;
}
int hungary()
{
int i,ans=0;
for(i=1;i<=n;i++)
{
memset(visit,0,sizeof(visit));
ans+=dfs(i);
}
return ans;
}
int main()
{
int i,j;
int a,b,m;
char c;
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
memset(f,0,sizeof(f));
for(i=0;i<n;i++)
{
scanf("%d%c%c%c%d%c",&a,&c,&c,&c,&m,&c);
for(j=0;j<m;j++)
{
scanf("%d",&b);
map[a+1][b+1]=1;
}
}
cout<<n-hungary()/2<<endl;
}
return 0;
}