杭电1068--Girls and Boys(二分图最大独立集)

Girls and Boys

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


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
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
 

 

Source
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1150 1151 1281 1507 1528 
二分图最大独立集 = 顶点数 - 最大匹配数。 
本题从整个点集搜索 、并没有将点集分开,相当于搜索了两遍。 所以最大匹配数要除以二。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int map[1010][1010], dis[1010], vis[1010];
 6 int n;
 7 bool Dfs(int a)
 8 {
 9     for(int i = 0; i < n; i++)
10     {
11         if(map[a][i] && !vis[i])
12         {
13             vis[i] = 1;
14             if(!dis[i] || Dfs(dis[i]))
15             {
16                 dis[i] = a;
17                 return true;
18             }
19         }
20     }
21     return false;
22 }
23 int main()
24 {
25     int q, m, GG; 
26     while(~scanf("%d", &n)) 
27     {
28         memset(map, 0, sizeof(map));
29         memset(dis, 0, sizeof(dis));
30         for(int i = 0; i < n; i++)
31         {
32             scanf("%d: (%d)", &q, &m);
33             for(int i = 1; i <= m; i++)
34             {
35                 scanf("%d", &GG);
36                 map[q][GG] = 1;
37             }
38         }
39         int sum = 0;
40         for(int i = 0; i < n; i++)
41         {
42             memset(vis, 0, sizeof(vis));
43             if(Dfs(i))
44                 sum++;
45         }
46         printf("%d
", n - sum/2);
47     }
48     return 0;
49 }
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int map[1010][1010], dis[1010], vis[1010];
 6 int n;
 7 bool Dfs(int a)
 8 {
 9     for(int i = 0; i < n; i++)
10     {
11         if(map[a][i] && !vis[i])
12         {
13             vis[i] = 1;
14             if(!dis[i] || Dfs(dis[i]))
15             {
16                 dis[i] = a;
17                 return true;
18             }
19         }
20     }
21     return false;
22 }
23 int main()
24 {
25     int q, m, GG; 
26     while(~scanf("%d", &n)) 
27     {
28         memset(map, 0, sizeof(map));
29         memset(dis, 0, sizeof(dis));
30         for(int i = 0; i < n; i++)
31         {
32             scanf("%d: (%d)", &q, &m);
33             for(int i = 1; i <= m; i++)
34             {
35                 scanf("%d", &GG);
36                 map[q][GG] = 1;
37             }
38         }
39         int sum = 0;
40         for(int i = 0; i < n; i++)
41         {
42             memset(vis, 0, sizeof(vis));
43             if(Dfs(i))
44                 sum++;
45         }
46         printf("%d
", n - sum/2);
47     }
48     return 0;
49 }
  
原文地址:https://www.cnblogs.com/soTired/p/4746260.html