Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8386 Accepted Submission(s): 3853
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
Source
这是一题让你求最大独立集问题!独立集即一个点集,集合中任
两个结点不相邻,则称V为独立集。
比如上述图片!只有a1,a2,b0,b1可以放在一起组成最大独立
集,不能再放其他的了!
二分图最大独立集=点数n-二分图最大匹配。
#include<cstdio>
#include<iostream>
#include<cstring>
const int mx=2000;
using namespace std;
int map[mx][mx];
int t;
int vis[mx];
int liked[mx];
int success(int x)
{
int i;
for(i=1;i<=t;i++)
{
if(!vis[i]&&map[x][i]==1)
{
vis[i]=1;
if(!liked[i]||success(liked[i]))
{
liked[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int i,a,b,c,num,sum;
while(cin>>t)
{
sum=t;
memset(map,0,sizeof(map));
memset(liked,0,sizeof(liked));
num=0;
while(sum--)
{
scanf("%d: (%d)",&a,&b);
while(b--)
{
cin>>c;
map[a+1][c+1]=1;
}
}
for(i=1;i<=t;i++)
{
memset(vis,0,sizeof(vis));
num+=success(i);
}
cout<<t-num/2<<endl;
}
}
对于这个问题!这个二分图是由一个集合组成的!
也可以说两个集合混在一起了
所以我们把这个集合复制一遍,最后得n*2-num;
但是这里应该把2*n换成n就成了n-num/2;