Girls and Boys
Time Limit: 5000MS |
|
Memory Limit: 10000K |
Total Submissions: 10912 |
|
Accepted: 4887 |
Description
In the second, 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 was necessary to find out the the maximum set satisfying the condition:there was no and the students in T He set who has been "romantically involved". The result of the program was 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 a 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
Source
Southeastern Europe
Title Link: http://poj.org/problem?id=1466
The main topic: Some men and some women have a relationship between the same sex, ask for a maximum set, so that there is no relationship between any two people
Topic Analysis: Bare maximum independent set problem, set up a binary graph, and then according to the maximum independent set = number-maximum matching, solve can, note that because the two-dimensional map is two-way set, the maximum match to find out 2
#include <cstdio> #include <cstring>int const MAX = 505;bool g[max][max];bool vis[max];int Cx[max], Cy[max]; int n;int DFS (int x) {for (int y = 0; y < n; y++) {if (!vis[y] && g[x][y]) {vis[ Y] = true; if (cy[y] = =-1 | | DFS (Cy[y])) {cy[y] = x; Cx[x] = y; return 1; }}} return 0;} int Maxmatch () {memset (CX,-1, sizeof (CX)); memset (CY,-1, sizeof (CY)); int res = 0; for (int i = 0; i < n; i++) {if (cx[i] = = 1) {memset (Vis, false, sizeof (VIS)); Res + = DFS (i); }} return res;} int main () {while (scanf ("%d", &n)! = EOF) {memset (g, false, sizeof (g)); for (int i = 0; i < n; i++) {int x; scanf ("%d", &x); int num; scanf (": (%d)", &num); for (int j = 0; j < Num; J + +) { int y; scanf ("%d", &y); G[x][y] = true; }} int ans = Maxmatch (); printf ("%d\n", N-ANS/2); }}
POJ 1466 Girls and Boys (the largest independent set of Hungarian algorithms)