二分图匹配/判断——The Accomodation of Students ( HDU 2444 )
来源:互联网 发布:海量数据的处理 编辑:程序博客网 时间:2024/06/05 18:17
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2444分析 && 题解:
需要判断是否为二分图并且如果是二分图,输出最大匹配。匈牙利算法加染色法判断即可PS:
需要注意的是,因为记录边的时候,同一条边被记录了两次,并且在匈牙利算法中,每次搜索的范围为所有点,所以每一对匹配都被算了两次,最后输出的时候应该除以2。AC代码:
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>using namespace std;int n, m;int Map[233][233];int col[233];int mark[233];bool flag[233];bool BFS(int s){ queue<int> q; q.push(s); col[s] = 1; while(!q.empty()) { int from = q.front(); q.pop(); for(int i=1; i<=n; i++) { if(Map[from][i] && col[i] == -1) { q.push(i); col[i] = !col[from]; } if(Map[from][i] && col[from]==col[i]) return false; } } return true;}int b,g;void Count(){ for(int i=1;i<=n;i++) { if(col[i]) b++; else g++; }}bool DFS(int x){ for(int i=1;i<=n;i++) { if(!Map[x][i]||flag[i]) continue; flag[i] = 1; if(!mark[i]||DFS(mark[i])) { mark[i] = x; return 1; } } return 0;}void Hungarian(){ int ans = 0; memset(mark, 0, sizeof(mark)); for(int i=1;i<=n;i++) { memset(flag, 0, sizeof(flag)); if(DFS(i)) { ans++; } } cout << ans/2 << endl;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(Map, 0, sizeof(Map)); for(int i=1;i<=m;i++) { int x,y; cin >> x>>y; Map[x][y] = 1; Map[y][x] = 1; } memset(col, -1, sizeof(col)); bool flag = false; for(int i=1;i<=n;i++) { if(col[i] == -1 && !BFS(i )) { flag = true; break; } } if(flag) { cout << "No" << endl; } else { b=0;g=0; Count(); Hungarian(); } } return 0;}
0 0
- 二分图匹配/判断——The Accomodation of Students ( HDU 2444 )
- HDU 2444 The Accomodation of Students 判断是否为二分图,二分图的最大匹配
- hdu 2444 The Accomodation of Students (判断二分图,二分图最大匹配)
- HDU - 2444 The Accomodation of Students(判断二分图 + 二分图匹配)
- hdu 2444 The Accomodation of Students(判断二分图,二分图最大匹配)
- hdu 2444 The Accomodation of Students(二分图判断,二分图最大匹配)
- HDU 2444 The Accomodation of Students(二分图判断+最大二分匹配)
- HDU 2444 The Accomodation of Students-二分图判断&二分匹配
- HDU-2444 The Accomodation of Students(二分图判断+最大二分匹配)
- HDU-2444-The Accomodation of Students [二分匹配][判断二分图][BFS染色法]
- hdu 2444 The Accomodation of Students 【二分图匹配】
- HDU 2444 The Accomodation of Students——二分图判定+最大匹配
- HDU 2444 — The Accomodation of Students 二分图+最大匹配
- hdu 2444 The Accomodation of Students(二分图判断)
- HDU 2444 The Accomodation of Students 判断二分图 + 最大匹配
- hdu 2444 The Accomodation of Students 二分图判断与匹配
- hdu 2444 The Accomodation of Students(二分图判断+求最大匹配)
- hdu 2444 The Accomodation of Students 二分图判断 + 最大匹配
- Kafka日志删除源码分析
- Xcode开发学习:入门1
- 汉字转拼音工具类
- 如何生成一个arff文件
- Kafka Producer是如何动态感知Topic分区数变化
- 二分图匹配/判断——The Accomodation of Students ( HDU 2444 )
- 内存屏障解析
- java 爬虫框架 webmagic
- APP开发实战96-动态Vector兼容性问题
- 动态添加菜单项
- Key为nulll时Kafka如何选择分区(Partition)
- 新闻资讯网站
- 重定向与转发 (附:压缩格式的本页显示)
- 文件相关工具类