hdu 1068 Girls and Boys (二分匹配)

Girls and Boys

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


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:  1507 1528 1054 1498 1083 
 

 

二分匹配入门题:

给出邻接矩阵和邻接表的做法:

一般数据大时邻接表会快很多。

 

邻接矩阵:

 1 //2437MS    4188K    907 B    C++
 2 #include<stdio.h>
 3 #include<string.h>
 4 #define N 1005
 5 int g[N][N];
 6 int match[N];
 7 int vis[N];
 8 int n;
 9 int dfs(int x)
10 {
11     for(int i=0;i<n;i++)
12         if(!vis[i] && g[x][i]){
13             vis[i]=1;
14             if(match[i]==-1 || dfs(match[i])){
15                 match[i]=x;
16                 return 1;
17             }
18         }
19     return 0;
20 }
21 int hungary()
22 {
23     int ret=0;
24     memset(match,-1,sizeof(match));
25     for(int i=0;i<n;i++){
26         memset(vis,0,sizeof(vis));
27         ret+=dfs(i);
28     }
29     return ret;
30 }
31 int main(void)
32 {
33     int a,m,b;
34     while(scanf("%d",&n)!=EOF)
35     {
36         memset(g,0,sizeof(g));
37         for(int i=0;i<n;i++){
38             scanf("%d: (%d)",&a,&m);
39             for(int j=0;j<m;j++){
40                 scanf("%d",&b);
41                 g[a][b]=g[b][a]=1;
42             }
43         }
44         printf("%d\n",n-hungary()/2);
45     } 
46     return 0;
47 }
View Code

邻接表:

 1 //203MS    268K    1169 B    C++
 2 #include<stdio.h>
 3 #include<string.h>
 4 #define N 1005
 5 struct node{
 6     int v;
 7     int next;
 8 }edge[10*N];
 9 int match[N];
10 int vis[N];
11 int head[N];
12 int n,edgenum;
13 void addedge(int u,int v)
14 {
15     edge[edgenum].v=v;
16     edge[edgenum].next=head[u];
17     head[u]=edgenum++;
18 }
19 int dfs(int x)
20 {
21     for(int i=head[x];i!=-1;i=edge[i].next){
22         int v=edge[i].v;
23         if(!vis[v]){
24             vis[v]=1;
25             if(match[v]==-1 || dfs(match[v])){
26                 match[v]=x;
27                 return 1;
28             }
29         }
30     }
31     return 0;
32 }
33 int hungary()
34 {
35     int ret=0;
36     memset(match,-1,sizeof(match));
37     for(int i=0;i<n;i++){
38         memset(vis,0,sizeof(vis));
39         ret+=dfs(i);
40     }
41     return ret;
42 }
43 int main(void)
44 {
45     int a,b,m;
46     while(scanf("%d",&n)!=EOF)
47     {
48         edgenum=0;
49         memset(head,-1,sizeof(head));
50         for(int i=0;i<n;i++){
51             scanf("%d: (%d)",&a,&m);
52             while(m--){
53                 scanf("%d",&b);
54                 addedge(a,b);
55                 addedge(b,a);
56             }
57         } 
58         printf("%d\n",n-hungary()/2);
59     }
60     return 0;
61 }
View Code

 

posted @ 2014-05-22 13:25  heaventouch  阅读(198)  评论(0编辑  收藏  举报