the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.
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.
学校对n个学生(男女都有)进行的调查了,发现了某些学生暗生情愫,现在需要你选出一个最大的集合,这个集合内部没有两个人暗生情愫。学生的编号是0~n-1
Input
输入包含多组数据
第一行一个整数n,表示学生的数量。
接下来n行,第一个数表示一个学生编号,接下来一个数表示与他产生联系的人的数量,接下来是一组编号表示产生联系的学生编号。具体格式见样例。
你可以认为,数据是对称的,且不会出现奇环。
Output
对于每组测试数据,输出一个整数,表示最多可以选出的人数。
Sample Input
7 0: (3) 4 5 6 1: (2) 4 6 2: (0) 3: (0) 4: (2) 0 1 5: (1) 0 6: (2) 0 1 3 0: (2) 1 2 1: (1) 0 2: (1) 0
Sample Output
5 2
题解:纯版子题二分图匹配
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int n;
const int maxn = 1e3+5;
int line[maxn][maxn];
int nxt[maxn];
int used[maxn];
bool find(int x)
{
for(int i = 0;i < n;++i)
{
if(line[x][i] && !used[i])
{
used[i] = 1;
if(nxt[i] == -1 || find(nxt[i]))
{
nxt[i] = x;
return true;
}
}
}
return false;
}
int match()
{
int sum = 0;
for(int i = 0;i < n;++i)
{
memset(used, 0, sizeof(used));
if(find(i))
{
sum++;
}
}
return sum;
}
int main()
{
while(~scanf("%d", &n))
{
memset(line, 0, sizeof(line));
memset(nxt, -1, sizeof(nxt));
for(int i = 0;i < n;++i)
{
int v;
scanf("%d", &v);
char v1[3];
cin>>v1;
char vv;
cin>>vv;
int g;
scanf("%d", &g);
cin>>vv;
int h;
while(g--)
{
scanf("%d", &h);
line[v][h] = 1;
}
}
cout << n - match()/2 << endl;
}
return 0;
}