Girls and Boys HDU

来源:互联网 发布:js脚本怎么写 编辑:程序博客网 时间:2024/05/20 23:29

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. 
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
Output
52


解题思路:该题要求的是最大独立集,因为题目中只告诉有几个人没说明谁是男的谁是女的,所以通过匈牙利算法算出的最大匹配数其实是有重复的,所以最后需要除2.最大独立集=顶点数-最大匹配数。

代码:

#include<stdio.h>#include<string.h>#define N 1010int line[N][N];int used[N],gril[N];int t;int find(int x){int i,j;for(i=0;i<t;i++){if(line[x][i]==1&&used[i]==0){used[i]=1;if(gril[i]==-1||find(gril[i])==1){gril[i]=x;return 1;}}}return 0;}int main(){int i,j,k,u,v,n;char s,s1,s2;while(scanf("%d",&t)!=EOF){memset(line,0,sizeof(line));for(i=0;i<t;i++){//0: (3) 4 5 6scanf("%d: (%d) ",&u,&n);//printf("%d %d\n",u,n);for(j=1;j<=n;j++){scanf("%d",&v);line[u][v]=1;//printf("%d ",v);}//printf("\n");}int c=0;memset(gril,-1,sizeof(gril));//因为人物编号是从0开始的所以这里初始值应该为-1.for(i=0;i<t;i++)//但是初始值为0也能过{memset(used,0,sizeof(used));int f=find(i);//printf("%d\n",f);if(f==1)c++;}printf("%d\n",t-c/2);}return 0;}