Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9926 Accepted Submission(s): 4554
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<cstdio>
#include<cstring>
using namespace std;
const int MAX = 1005;
int Map[MAX][MAX], vis[MAX], link[MAX];
int n, a, m, b, sum;
bool PP(int node)
{
for(int i = 0;i < n; ++i)
{
if(!vis[i] && Map[node][i])
{
vis[i] = 1;
if(link[i] == -1 || PP(link[i]))
{
link[i] = node;
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d", &n) != EOF)
{
sum = 0;
memset(Map, 0, sizeof(Map));
for(int i = 0;i < n; ++i) link[i] = -1;
for(int i = 0;i < n; ++i)
{
scanf("%d: (%d)", &a, &m);
while(m--)
{
scanf("%d", &b);
Map[a][b] = 1;
}
}
for(int i = 0;i < n; ++i)
{
memset(vis, 0, sizeof(vis));
if(PP(i)) sum ++;
}
cout << n-sum/2 << endl;
}
return 0;
}