Girls and Boys

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;
 } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章