Girls and BoysTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5966 Accepted Submission(s): 2672
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
Sample Output
Source
Recommend
JGShining
|
=====================================题目大意=====================================
大学第二年有人开始研究男女同学之间的romantic关系。
而找出相互之间没有romantic关系的最大学生集体对于研究来说是十分必要的。
程序的答案也就是这个集体中的学生数量。
=====================================算法分析=====================================
建图过程很简单,将学生看做点,romantic关系看做边。
而“在所有点中选取尽可能多的点使得选取的点两两之间不存在边”等价于“在所有点中去除尽可能少的点使得剩下的点两两之间不
存在边”。
这也就是求最小点覆盖数,因为去除覆盖点之后便去除了所有边。
然后根据二分图最大匹配的König定理“最小点覆盖数 = 最大匹配数”可知本题答案即“节点数 - 最大匹配数”。
事实这个结论可以直接用定理“二分图的最大独立集 = 节点数 - 最大匹配数”得出(独立集指两两之间没有边的顶点集,最大独立
集即顶点最多的独立集)。
注意如果令二分图的U点集 = V点集 = 全体学生,那么最终答案应该是“N - 最大匹配数 / 2”,因为每一条边都被计算了
两次。
当然也可以用一个DFS根据性别将学生分为两个集合分别作为二分图的两个点集,这样答案就是“N - 最大匹配数”。
=======================================代码=======================================
#include<stdio.h>
#include<string.h>
const int MAXN=505;
int N,Linker[MAXN];
bool Edge[MAXN][MAXN],Vis[MAXN];
bool DFS(int U)
{
for(int v=0;v<N;++v)
{
if(Edge[U][v]&&!Vis[v])
{
Vis[v]=true;
if(Linker[v]==-1||DFS(Linker[v]))
{
Linker[v]=U;
return true;
}
}
}
return false;
}
int Hungary()
{
memset(Linker,-1,sizeof(Linker));
int ans=0;
for(int u=0;u<N;++u)
{
memset(Vis,0,sizeof(Vis));
if(DFS(u)) { ++ans; }
}
return ans;
}
int main()
{
while(scanf("%d",&N)==1)
{
memset(Edge,0,sizeof(Edge));
for(int i=0;i<N;++i)
{
int a,num;
scanf("%d: (%d)",&a,&num);
while(num--)
{
int b;
scanf("%d",&b);
Edge[a][b]=true;
}
}
printf("%d\n",N-Hungary()/2);
}
return 0;
}