hdu1068——Girls and Boys

来源:互联网 发布:侠客风云传 招式数据 编辑:程序博客网 时间:2024/06/05 17:07

Girls and Boys

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7681    Accepted Submission(s): 3518


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
 

Source
Southeastern Europe 2000
 

Recommend

求最大独立集 = node - 最大匹配数

#include<map>      #include<set>      #include<list>      #include<stack>      #include<queue>      #include<vector>      #include<cmath>      #include<cstdio>      #include<cstring>      #include<iostream>      #include<algorithm>            using namespace std;  bool vis[1010];int mark[1010];int m;struct node2{int next;int to;}edge[1010 * 1010];int head[1010];int tot;void addedge(int from, int to){edge[tot].to = to;edge[tot].next = head[from];head[from] = tot++;}bool dfs(int u){for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(!vis[v]){vis[v] = 1;if(mark[v] == -1 || dfs(mark[v])){mark[v] = u;return true;}} }return false;}int hungary(){int ans = 0;memset( mark, -1, sizeof(mark) );for(int i = 0; i < m; i++){memset( vis, 0, sizeof(vis) );if(dfs(i))ans++;}return ans;}int main(){while(~scanf("%d", &m)){int c;int j, x;memset( head, -1, sizeof(head) );tot = 0;for(int i = 0; i < m; i++){getchar();scanf("%d: (%d)", &x, &c);if( !c)continue;while(c--){scanf("%d", &j);addedge(i, j);}}printf("%d\n", m - hungary() / 2);}return 0;}


0 0