Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9560 Accepted Submission(s): 4379
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.
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
给出每个人跟别人的所有关系,现找出一些人构成一个集体,要求任意两个人都没有关系,求这个集体最多有多少个人....
说句实话,也没想到是怎么做的,只感觉到要用到最大匹配的思想,但是其他的不知道怎么处理了,后来看了大神的解析,原来有一个公式:
最大独立集合的数量=顶点数-最大匹配数
然后就是无脑的匈牙利算法模板求最大匹配数了,当然,因数据给出的原因,求得的匹配的数量求得的是真实的匹配量的二倍,需要除以二,以防超时,用了邻接表......
ps:看到很多人2000Ms+的时间A掉的,时间限制是10kMs,估计暴力也能过的吧.....
#include<stdio.h>
#include<string.h>
const int maxn=10005;
int link[maxn],vis[maxn],head[maxn],cnt;
struct Edge
{
int to,next;
}edge[maxn*maxn];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
memset(link,-1,sizeof(link));
}
void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int 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(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return 1;
}
}
}
return 0;
}
int match(int n)//匹配
{
int ans=0;
for(int i=0;i<n;++i)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
{
++ans;
}
}
return ans;
}
int main()
{
int n,m;
//freopen("shuju.txt","r",stdin);
while(~scanf("%d",&n))
{
init();
for(int i=0;i<n;++i)
{
int m,a,b;
scanf("%d: (%d)",&a,&m);//注意格式
while(m--)
{
scanf("%d",&b);
add(a,b);add(b,a);
}
}
int tp=match(n);
printf("%d\n",n-tp/2);
}
return 0;
}