HDU2444(KB10-B 二分图判定+最大匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6983 Accepted Submission(s): 3120
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.
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.
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
题意:判定是否为二分图,若是,输出最大匹配
1 //2017-08-21 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 const int N = 210; 9 int head[N], tot; 10 struct Edge{ 11 int to, next; 12 }edge[N*N]; 13 14 void init(){ 15 tot = 0; 16 memset(head, -1, sizeof(head)); 17 } 18 19 void add_edge(int u, int v){ 20 edge[tot].to = v; 21 edge[tot].next = head[u]; 22 head[u] = tot++; 23 24 edge[tot].to = u; 25 edge[tot].next = head[v]; 26 head[v] = tot++; 27 } 28 29 int n, m; 30 string G[N]; 31 int matching[N]; 32 int check[N]; 33 34 bool dfs(int u){ 35 for(int i = head[u]; i != -1; i = edge[i].next){ 36 int v = edge[i].to; 37 if(!check[v]){//要求不在交替路 38 check[v] = 1;//放入交替路 39 if(matching[v] == -1 || dfs(matching[v])){ 40 //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功 41 matching[u] = v; 42 matching[v] = u; 43 return true; 44 } 45 } 46 } 47 return false;//不存在增广路 48 } 49 50 //hungarian: 二分图最大匹配匈牙利算法 51 //input: null 52 //output: ans 最大匹配数 53 int hungarian(){ 54 int ans = 0; 55 memset(matching, -1, sizeof(matching)); 56 for(int u = 1; u <= n; u++){ 57 if(matching[u] == -1){ 58 memset(check, 0, sizeof(check)); 59 if(dfs(u)) 60 ans++; 61 } 62 } 63 return ans; 64 } 65 66 int col[N]; 67 68 //color: 黑白染色二分图判定 69 //output: true 是二分图, false 不是二分图 70 bool color(int u){ 71 for(int i = head[u]; i != -1; i = edge[i].next){ 72 int v = edge[i].to; 73 if(!col[v]){ 74 col[v] = !col[u]; 75 if(!color(v))return false; 76 }else if(col[v] == col[u]) 77 return false; 78 } 79 return true; 80 } 81 82 int main() 83 { 84 //freopen("inputB.txt", "r", stdin); 85 while(scanf("%d%d", &n, &m)!=EOF){ 86 //考虑只有一个人时的特殊情况 87 if(n == 1){ 88 printf("No\n"); 89 continue; 90 } 91 init(); 92 int u, v; 93 for(int i = 0; i < m; i++){ 94 scanf("%d%d", &u, &v); 95 add_edge(u, v); 96 } 97 memset(col, 0, sizeof(col)); 98 col[1] = 1; 99 if(color(1)) 100 printf("%d\n", hungarian()); 101 else 102 printf("No\n"); 103 } 104 105 return 0; 106 }