码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1068 Girls and Boys (最大独立集)

时间:2018-08-12 13:59:47      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:book   integer   pac   cts   ems   win   form   inf   should   

Girls and Boys
Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13556    Accepted Submission(s): 6385

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

 

C/C++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0xffffff
12 using namespace std;
13 const int my_max = 1005;
14 
15 int n, m, a, b, my_G[my_max][my_max], my_book[my_max], my_right[my_max];
16 
17 bool my_dfs(int x)
18 {
19     for (int i = 0; i < n; ++ i)
20     {
21         if (my_G[x][i] && !my_book[i])
22         {
23             my_book[i] = 1;
24             if (!my_right[i] || my_dfs(my_right[i]))
25             {
26                 my_right[i] = x;
27                 return true;
28             }
29         }
30     }
31     return false;
32 }
33 
34 int my_hungarian()
35 {
36     int my_cnt = 0;
37     for (int i = 0; i < n; ++ i)
38     {
39         memset (my_book, 0, sizeof(my_book));
40         if (my_dfs(i))
41             ++ my_cnt;
42     }
43     return my_cnt / 2;
44 }
45 
46 int main()
47 {
48     while (~scanf("%d", &n))
49     {
50         memset(my_right, 0, sizeof(my_right));
51         memset(my_G, 0, sizeof(my_G));
52         for (int i = 0; i < n; ++ i)
53         {
54             scanf("%d: (%d)", &a, &m);
55             while (m --)
56             {
57                 scanf("%d", &b);
58                 my_G[a][b] = 1;
59             }
60         }
61 
62         printf("%d\n", n - my_hungarian());
63     }
64     return 0;
65 }

 

hdu 1068 Girls and Boys (最大独立集)

标签:book   integer   pac   cts   ems   win   form   inf   should   

原文地址:https://www.cnblogs.com/GetcharZp/p/9462268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!