博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2444(KB10-B 二分图判定+最大匹配)
阅读量:4568 次
发布时间:2019-06-08

本文共 3872 字,大约阅读时间需要 12 分钟。

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.
 

 

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

 
题意:判定是否为二分图,若是,输出最大匹配
1 //2017-08-21  2 #include 
3 #include
4 #include
5 #include
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 else102 printf("No\n");103 }104 105 return 0;106 }

 

转载于:https://www.cnblogs.com/Penn000/p/7403866.html

你可能感兴趣的文章
底层原理
查看>>
21. Merge Two Sorted Lists
查看>>
创建数组
查看>>
dict使用
查看>>
ASP.NET MVC的帮助类HtmlHelper和UrlHelper
查看>>
02_ListActive中响应事件 并LogCat输出
查看>>
doubleclick adx note
查看>>
Celery框架
查看>>
[c#]asp.net开发微信公众平台(4)关注事件、用户记录、回复文本消息
查看>>
[转载,感觉写的非常详细]DUBBO配置方式详解
查看>>
linux Valgrind使用说明-内存泄漏
查看>>
Android在Eclipse上的环境配置
查看>>
面向对象(五)
查看>>
android平台下使用点九PNG技术
查看>>
Python学习3,列表
查看>>
最长回文子串
查看>>
JAVA基础-JDBC(一)
查看>>
js中for和while运行速度比较
查看>>
算法第5章作业
查看>>
7.9 练习
查看>>