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.
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.
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
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;}
阅读全文
0 0
- Girls and Boys HDU
- hdu 2690 Boys and girls
- HDU-1068 Girls and Boys
- hdu 1068 Girls and Boys
- hdu 1068 Girls and Boys
- hdu 1068 Girls and Boys
- HDU 1068 Girls and Boys
- hdu 1068 Girls and Boys
- HDU 1068 Girls and Boys
- HDU 2690 Boys and girls
- hdu 1068 Girls and Boys
- HDU 1068Girls and Boys
- HDU 1068 Girls and Boys
- hdu 1068 Girls and Boys
- 【hdu 1068】Girls and Boys
- hdu 1068 Girls and Boys 二分匹配
- hdu 1068 Girls and Boys 解题报告
- hdu 1068 Girls and Boys 匈牙利
- java 静态方法和实例方法的区别
- Java 用反射将JavaBean转成Map
- java学习之Iterator(迭代器)的基础用法(转)
- (19)备份与恢复、导入导出
- PHP内置的字符串长度函数strlen mb_strlen
- Girls and Boys HDU
- UVA1641ASCIIArea
- idea上把maven项目打包成war包直接部署在tomcat上
- 模板方法模式 template method pattern
- 浏览器的工作流程
- java中String、StringBuffer、StringBuilder的区别
- stimusoft reports 中图片显示问题
- jquery批量获取form表单要提交的值
- 前后端分离之本地数据调试——mockJs的应用