现在的位置: 首页 > 综合 > 正文

hdu1068——Girls and Boys

2019年02月18日 ⁄ 综合 ⁄ 共 2010字 ⁄ 字号 评论关闭

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7681    Accepted Submission(s): 3518

Problem Description
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.

 

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
 

Source
 

Recommend

求最大独立集 = node - 最大匹配数

#include<map>      
#include<set>      
#include<list>      
#include<stack>      
#include<queue>      
#include<vector>      
#include<cmath>      
#include<cstdio>      
#include<cstring>      
#include<iostream>      
#include<algorithm>      
      
using namespace std;  

bool vis[1010];
int mark[1010];
int m;

struct node2
{
	int next;
	int to;
}edge[1010 * 1010];
int head[1010];
int tot;

void addedge(int from, int to)
{
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

bool dfs(int u)
{
	for(int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if(!vis[v])
		{
			vis[v] = 1;
			if(mark[v] == -1 || dfs(mark[v]))
			{
				mark[v] = u;
				return true;
			}
		} 
	}
	return false;
}

int hungary()
{
	int ans = 0;
	memset( mark, -1, sizeof(mark) );
	for(int i = 0; i < m; i++)
	{
		memset( vis, 0, sizeof(vis) );
		if(dfs(i))
			ans++;
	}
	return ans;
}

int main()
{
	while(~scanf("%d", &m))
	{
		int c;
		int j, x;
		memset( head, -1, sizeof(head) );
		tot = 0;
		for(int i = 0; i < m; i++)
		{
			getchar();
			scanf("%d: (%d)", &x, &c);
			if( !c)
				continue;
			while(c--)
			{
				scanf("%d", &j);
				addedge(i, j);
			}
		}
		printf("%d\n", m - hungary() / 2);
	}
	return 0;
}

抱歉!评论已关闭.