HDU 1068 Girls and Boys二分图最大匹配
2015-08-23 22:39
309 查看
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
Sample Output
记得除以2!!!因为重复加边了~~好好处理括号,真是过分,搞ACM大半年,回回碰到字符串就卡壳
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
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
记得除以2!!!因为重复加边了~~好好处理括号,真是过分,搞ACM大半年,回回碰到字符串就卡壳
#include<cstdio> #include<cstring> using namespace std; /* ************************************************************************** //二分图匹配(匈牙利算法的DFS实现) //初始化:g[][]两边顶点的划分情况 //建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 //g没有边相连则初始化为0 //uN是匹配左边的顶点数,vN是匹配右边的顶点数 //调用:res=hungary();输出最大匹配数 //优点:适用于稠密图,DFS找增广路,实现简洁易于理解 //时间复杂度:O(VE) //***************************************************************************/ //顶点编号从0开始的 const int MAXN=500; int uN,vN;//u,v数目 int g[MAXN][MAXN]; int linker[MAXN]; bool used[MAXN]; int k,m,n,u,v; bool dfs(int u)//从左边开始找增广路径 { int v; for(v=1;v<=n;v++)//这个顶点编号从0开始,若要从1开始需要修改 if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) {//找增广路,反向 linker[v]=u; return true; } } return false;//这个不要忘了,经常忘记这句 } int hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=1;u<=n;u++) { memset(used,0,sizeof(used)); if(dfs(u)) res++; } return res; } //******************************************************************************/ int main() { //freopen("cin.txt","r",stdin); while(~scanf("%d",&n)) { memset(g,0,sizeof(g)); for(int i=1;i<=n;i++) { scanf("%d: (%d)",&k,&u);/// // printf("%d ",u); while(u--) { scanf("%d",&v); //printf("%d ",v); g[i][v+1]=1; } // puts(""); } // memset(linker,-1,sizeof(linker)); printf("%d\n",n-hungary()/2); } return 0; }
相关文章推荐
- 【剑指offer】二,变态跳台阶(java实现)
- 【.Net】关于内存缓存的一点记录
- 数组的排序算法
- iava多线程详解(2)-成员变量与局部变量访问
- Word Break
- 如何在Mac上配置/使用Github
- 解决: g++: internal compiler error: Killed (program cc1plus)
- 转载:PostgreSQL 内部概貌
- 关于html中li的问题
- Linux串口编程详解
- Scala中类的继承,超类的重载等
- js知识点
- oracle修改密码、添加用户及授权
- 第四周1000题解
- <Android Framework 之路>Android5.1 Camera Framework(二)
- TOM大叔的几道Javascript题目与解答
- perl IO::Socket::INET模块
- Android5.1 Camera Framework(二)
- hdu2089 不要62(暴力打表计数,数位DP)
- swtich与if else是否能作用在byte上,作用在long上,作用在String上?