【hdu 1068】Girls and Boys

来源:互联网 发布:淘宝女装店铺图片 编辑:程序博客网 时间:2024/05/17 22:53

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.
 

Sample Input
70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
 

Sample Output
52



题意:
在已给出的关系中,找出一个最大的集合使得其中的任意男女之间没有“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.
 

Sample Input
70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
 

Sample Output
52
原创粉丝点击