HDU2444 The Accomodation of Students —— 二分图最大匹配

题目链接:https://vjudge.net/problem/HDU-2444

 

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7328    Accepted Submission(s): 3270


Problem Description
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.
 

 

Input
For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.

Proceed to the end of file.

 

 

Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
 

 

Sample Input
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
 

 

Sample Output
No 3
 

 

Source
 

 

Recommend
gaojie

 

 

 

题解:

任务1:能否把这些人分成两组,且在每一组内,所有人互不相识(即两点间没有边直接相连)?

任务2:求最大匹配数, 直接用hungary()算法。

1.由于要把所有点分成两组,所以我们可以用两种颜色,对整幅图进行染色。规定:相邻两点间的颜色不同,然后把颜色相同的归为一组。

2.对于没有被染色的点u,对其进行染色,然后遍历所有与之相连的点v,如果点v没有被染色,则对其进行访问,染上另外一种颜色;如果点v已经被染色,则根据点u和点v的的染色情况来判断是否有冲突:

3.如果颜色相同,即把他们放在同一组,但他们是相互认识的,不能放在同一组,所以产生了冲突;如果颜色不同,则他们被分在了两组,符合要求。

 

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 const int INF = 2e9;
14 const int MOD = 1e9+7;
15 const int MAXN = 200+10;
16 
17 int n;
18 char a[MAXN][MAXN];
19 int M[MAXN][MAXN], link[MAXN];
20 bool vis[MAXN];
21 
22 bool dfs(int u)
23 {
24     for(int i = 1; i<=n; i++)
25     if(M[u][i] && !vis[i])
26     {
27         vis[i] = true;
28         if(link[i]==-1 || dfs(link[i]))
29         {
30             link[i] = u;
31             return true;
32         }
33     }
34     return false;
35 }
36 
37 int hungary()
38 {
39     int ret = 0;
40     memset(link, -1, sizeof(link));
41     for(int i = 1; i<=n; i++)
42     {
43         memset(vis, 0, sizeof(vis));
44         if(dfs(i)) ret++;
45     }
46     return ret;
47 }
48 
49 int col[MAXN];
50 bool Color(int u, int c)    //染色,如果有冲突,则返回true
51 {
52     col[u] = c;
53     for(int i = 1; i<=n; i++)
54     if(M[u][i])
55     {
56         if(col[i]==col[u]) return true; //与之前访问过的点相连,且为同色,则有冲突。
57         if(col[i]==-1 && Color(i, !c))  return true;    //如果没有没有访问过,则对其染色。
58     }
59     return false;
60 }
61 
62 int main()
63 {
64     int m;
65     while(scanf("%d%d", &n, &m)!=EOF)
66     {
67         memset(M, false, sizeof(M));
68         for(int i = 1; i<=m; i++)
69         {
70             int u, v;
71             scanf("%d%d", &u, &v);
72             M[u][v] = M[v][u] = true;
73         }
74 
75         bool flag = false;
76         memset(col, -1, sizeof(col));
77         for(int i = 1; i<=n; i++)   //染色
78             if(col[i]==-1)
79                 flag = flag|Color(i, 0);
80 
81         if(flag)
82         {
83             printf("No\n");
84             continue;
85         }
86 
87         int cnt = hungary();
88         printf("%d\n", cnt/2);
89     }
90 }
View Code

 

posted on 2017-11-11 09:04  h_z_cong  阅读(200)  评论(0编辑  收藏  举报

导航