Girls and Boys
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12134 Accepted Submission(s): 5715
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
题意:
在已给出的关系中,找出一个最大的集合使得其中的任意男女之间没有“romantically involved”。
最大独立集合=顶点数-最大匹配数
因为题目中没有给出男女,所以需要双向建图
最后的答案中ant要除以2
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int f[505];
bool fr[505][505];
bool vis[505];
int n,m,k;
int find(int x)
{
for(int i=0;i<n;i++)
{
if(fr[x][i]==true&&!vis[i])
{
vis[i]=true;
if(f[i]==-1)
{
f[i]=x;
return true;
}
else if(find(f[i]))
{
f[i]=x;
return true;
}
}
}
return false;
}
int main()
{
while(~scanf("%d",&n))
{
memset(f,-1,sizeof(f));
memset(fr,false,sizeof(fr));
for(int i=0;i<n;i++)
{
int m,k;
scanf("%d: (%d)",&m,&k);
for(int i=0;i<k;i++)
{
int t;
scanf("%d",&t);
fr[m][t]=true;
fr[t][m]=true;
}
}
int ant=0;
for(int i=0;i<n;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))
ant++;
}
printf("%d\n",n-(ant/2));
}
return 0;
}
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