Girls and Boys

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


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
JGShining
 
 

这题大概意思就是说找出一个最大的集合使得该集合的任意两个人木有关系。

根据最大独立集 =顶点数 - 最大匹配数

由于题目没有给出哪些是男的哪些是女的,也就是说没有明显的二分图,所以将一个人拆成两个人进行最大匹配。由于一个拆成两个,所以最大匹配数应该是求出来的数除以2 。最后再用顶点数减就行了。

用邻接表,上代码

主要是匈牙利算法:

#include<stdio.h>
#include
<string.h>
const int MAXN=1000;
int map[MAXN][MAXN];
int n;
int linker[MAXN];
bool used[MAXN];
bool dfs(int a)
{
for(int i=0;i<n;i++)
if(map[a][i]&&!used[i])
{
used[i]
=true;
if(linker[i]==-1||dfs(linker[i]))
{
linker[i]
=a;
return true;
}
}
return false;
}
int hungary()
{
int result=0;
memset(linker,
-1,sizeof(linker));
for(int i=0;i<n;i++)
{
memset(used,
0,sizeof(used));
if(dfs(i)) result++;
}
return result;
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
int i,j,num,a,b;
while(scanf("%d",&n)!=EOF)
{
memset(map,
0,sizeof(map));
for(i=1;i<=n;i++)
{
scanf(
"%d: (%d)",&a,&num);
for(j=0;j<num;j++)
{
scanf(
"%d",&b);
map[a][b]
=1;
}
}
int cnt=hungary();
printf(
"%d\n",n-cnt/2);

}
return 0;
}