二分图最大独立集
来源:互联网 发布:淘宝代购mk的正品店 编辑:程序博客网 时间:2024/05/01 21:42
http://poj.org/problem?id=1466
Girls and Boys
Time Limit: 5000MS Memory Limit: 10000KTotal Submissions: 10206 Accepted: 4525
Description
In 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.
Input
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 (n <=500 ), for n subjects.
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 (n <=500 ), for n subjects.
Output
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题意:
列出每两对人之间是否有罗曼史,然后求一个最大的集合,使得这个集合中的每两个人之间都没有罗曼史
最大独立集=总点数-最大匹配(最小点覆盖)
分析:本题中男女性别未知,把所有人建立左边的点,所有人建立右边的点,然后连线,此时得到的最大匹配是实际上的2倍,有重复的
所以:最大独立集=N-最大匹配数/2;
程序:
#include"string.h"#include"stdio.h"#include"iostream"#include"queue"#include"string"#include"map"#define M 555#define inf 999999999using namespace std;int G[555][555],y[555],use[555],x[555];int finde(int u,int n){ int i; for(i=1;i<=n;i++) { if(!use[i]&&G[u][i]) { use[i]=1; if(y[i]==0||finde(y[i],n)) { y[i]=u; x[u]=i; return 1; } } } return 0;}int max_match(int n){ memset(y,0,sizeof(y)); memset(x,0,sizeof(x)); int ans=0; for(int i=1;i<=n;i++) { if(!x[i]) { memset(use,0,sizeof(use)); ans+=finde(i,n); } } return ans;}int main(){ int n,i,a,b,j; char ch[222]; while(scanf("%d",&n)!=-1) { memset(G,0,sizeof(G)); for(i=1;i<=n;i++) { scanf("%d:",&a); scanf("%s",ch); int m=strlen(ch); int k=0; for(j=1;j<m-1;j++) k=10*k+ch[j]-'0'; while(k--) { scanf("%d",&b); G[a+1][b+1]=1; } } int ans=max_match(n); printf("%d\n",n-ans/2); }}
0 0
- 二分图最大独立集
- 二分图最大独立集
- 二分图最大独立集
- 二分图最大匹配 & 最大独立集
- hdu 3829 二分图最大独立集
- poj 2771 二分图最大独立集
- HDU2768二分图求最大独立集
- poj3692 Kindergarten 二分图最大独立集
- hdu4160 Dolls (二分图最大独立集)
- poj 1466 二分图 最大独立集
- 二分图最大独立集--poj2771
- 二分图最大独立集-poj1466
- poj1466 二分图最大独立集
- HDU1054Strategic Game【二分图最大独立集】
- poj3020 二分图匹配 最大独立集
- 二分图(最大独立集)
- poj2771 二分图的最大独立集
- hdu3829 二分图的最大独立集
- Java学习之---简单的JDBC连接Oracle
- 判断字符串s2能否由s1旋转得到
- 几个简单的正则表达式简介
- UVa 10474 - Where is the Marble?
- 编程精粹--编写高质量C语言代码(4):为子系统设防(一)
- 二分图最大独立集
- 微信公众帐号开发教程第17篇-应用实例之智能翻译
- 常用算法思想简介
- Android Call requires API level 11 (current min is 8)的解决方案
- poj 2311 Cutting Game sg函数
- 关于nil和 null和NSNull的区别
- appManifes
- Step by step use kali linux OS
- java文件随机读写--RandomAccessFile