HDU1068--Girls and Boys

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


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

求最大独立集合=N-最大匹配

因为这里的最大匹配为两次

所以答案最大独立集合=N-最大匹配/2;

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 int n;
 9 int cx[1001],cy[1001];
10 int mk[1001];
11 int link[1001][1001];
12 
13 void init()
14 {
15     memset(cx,0xff,sizeof(cx));
16     memset(cy,0xff,sizeof(cy));
17     memset(link,0,sizeof(link));
18 }
19 
20 int path(int u)
21 {
22     int v;
23     for(v=0;v<n;v++)
24     {
25         if(!mk[v]&&link[u][v])
26         {
27             mk[v]=1;
28             if(cy[v]==-1||path(cy[v]))
29             {
30                 cy[v]=u;
31                 cx[u]=v;
32                 return 1;
33             }
34         }
35     }
36     return 0;
37 }
38 
39 int find()
40 {
41     int sum=0;
42     int i;
43     for(i=0;i<n;i++)
44     {
45         if(cx[i]==-1)
46         {
47             memset(mk,0,sizeof(mk));
48             sum+=path(i);
49         }
50     }
51     return sum;
52 }
53 
54 int main()
55 {
56     while(scanf("%d",&n)!=EOF&&n!=0)
57     {
58         init();
59         int i;
60         for(i=0;i<n;i++)
61         {
62             int s,e,num;
63             scanf("%d: (%d)",&s,&num);
64             while(num--)
65             {
66                 scanf("%d",&e);
67                 link[s][e]=1;
68             }
69         }
70         int ans=find();
71         printf("%d
",n-ans/2);
72     }
73     return 0;
74 }
View Code
原文地址:https://www.cnblogs.com/zafuacm/p/3216342.html