Q - Girls and Boys

来源poj1068

In 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.

Input

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 (n <=500 ), for n subjects.

Output

For each given data set, the program should write to standard output a line containing the result.

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

最大独立集合,但要男女,而男女没有给出,所以会重复,要除2;点数-最大匹配数/2

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1005;
int n,m,x,y;
int pre[N],line[N][N],visit[N],connect[N];
bool find(int x)
{
	rep(i,1,n+1)
	{
		if(visit[i]==0&&line[x][i])
		{
			visit[i]=1;
			if((pre[i]==0||find(pre[i]))&&pre[i]!=x)
			{
				pre[i]=x;
				return true;
			}
		}
	}
	return false;
}
//int deal()
//{
//	int ans=0;
//	rep(i,1,n+1)
//	{
//		if(pre[pre[i]]==i)
//		{
//			ans++;
//			pre[i]=-1;
//		}
//	}
//	return ans;
//}
int main()
{
	while(~sf("%d",&n))
	{
		mm(line,0);
		mm(pre,0);
		rep(i,1,n+1)
		{
			int p;	
			sf("%d: (%d)",&x,&p);
			while(p--)
			{
				sf("%d",&y);
				line[x+1][y+1]=1;
			}
		}
		int ans=0;
		rep(i,1,n+1)
		{
			mm(visit,0);
			if(find(i)) ans++;
		}
		ans/=2;
	//	ans+=deal();
		pf("%d\n",n-ans);
	}
	return 0;
 } 
posted @ 2018-08-11 10:00  一无所知小白龙  阅读(218)  评论(0编辑  收藏  举报