POJ 2942 圆桌骑士 点双连通+二分图判定
来源:互联网 发布:win7无法保存网络凭证 编辑:程序博客网 时间:2024/04/28 21:17
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>#include<math.h>#include<stack>#include<vector>#define N 1005#define M 1000005using namespace std;struct Edge{int from, to;};int n, m;int pre[N], iscut[N], bccno[N], dfs_clock, bcc_cnt;vector<int> G[N], bcc[N];stack<Edge>S;void addedge(int u, int v){G[u].push_back(v); }int dfs(int u, int fa){int lowu = pre[u] = ++dfs_clock;int child = 0;for(int i = 0; i < G[u].size(); i++){int v = G[u][i];Edge e = {u, v};if(!pre[v]){S.push(e);child++;int lowv = dfs(v, u);lowu = min(lowu, lowv);if(lowv >= pre[u]){iscut[u] = true;bcc_cnt++; bcc[bcc_cnt].clear(); //连通块标号从1开始while(1){Edge x = S.top(); S.pop();if(bccno[x.from] != bcc_cnt){ bcc[bcc_cnt].push_back(x.from); bccno[x.from] = bcc_cnt;}if(bccno[x.to] != bcc_cnt){ bcc[bcc_cnt].push_back(x.to); bccno[x.to] = bcc_cnt;}if(x.from == u && x.to == v)break;}}}else if(pre[v] < pre[u] && v!=fa){S.push(e);lowu = min(lowu, pre[v]);}}if(fa < 0 && child == 1)iscut[u] = 0;return lowu;}void find_bcc(int n){memset(pre, 0, sizeof(pre));memset(iscut, 0, sizeof(iscut));memset(bccno, 0, sizeof(bccno));dfs_clock = bcc_cnt = 0;for(int i = 0; i < n; i++)if(!pre[i]) dfs(i, -1);}int map[N][N];int odd[N], color[N];bool bipartite(int u, int b){for(int i = 0; i < G[u].size(); i++){int v = G[u][i]; if(bccno[v] != b) continue;if(color[v] == color[u]) return false;if(!color[v]){color[v] = 3 - color[u];if(!bipartite(v, b))return false;}}return true; }void init(){for(int i = 1; i <= n; i++)G[i].clear(), memset(map[i], 0, sizeof(map[i]));memset(odd, 0, sizeof(odd));}int main(){int u, v, Cas = 1;while(scanf("%d %d",&n,&m), n+m){init();while(m--){scanf("%d %d", &u, &v);map[u][v] = map[v][u] = 1;}for(int i = 1; i <= n; i++)for(int j = i+1; j <= n; j++)if(!map[i][j])addedge(i, j), addedge(j, i);find_bcc(n);for(int i = 1; i <= bcc_cnt; i++) {memset(color, 0, sizeof(color));for(int j = 0; j < bcc[i].size(); j++) bccno[bcc[i][j]] = i;int u = bcc[i][0];color[u] = 1;if(!bipartite(u, i))for(int j = 0; j < bcc[i].size(); j++) odd[bcc[i][j]] = 1;}int ans = n;for(int i = 1; i <= n; i++) if(odd[i]) ans--;printf("%d\n", ans);}return 0;}/*5 51 41 52 53 44 55 113 12 42 11 11 31 12 11 31 14 35 85 85 31 24 23 52 15 25 22 46 81 41 51 62 42 52 63 64 5ans: 2 0 1 3*/
0 0
- POJ 2942 圆桌骑士 点双连通+二分图判定
- poj 2942 圆桌骑士 二分图判定判奇圈,点双连通分量 大白经典题目
- poj 2942-圆桌骑士(点双连通分量+二分图)
- [POJ]2942 圆桌骑士 点双连通分量 + 二分图染色
- poj2942圆桌骑士【点双连通分量+二分图判断】
- poj2942圆桌骑士(点双连通分量+二分图染色法判奇圈)
- 双连通分量,二分图(圆桌骑士,LA 3523)
- LA3523.Knights of the Round Table圆桌骑士——点双连通分量+二分图判断
- poj 2942 Knights of the Round Table 补图+点双连通分量+判定二分图
- POJ 2942 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- poj2942 点双连通图分量+二分图判定
- LA 3523 圆桌骑士 (双连通)
- 【poj2942】圆桌骑士Knights of the Round Table【双连通分量】【二分图】【奇圈】
- poj 2942 圆桌武士 双连通分量(BCC)+二分图+奇圈判断
- POJ 2942 圆桌骑士问题。
- 【连通图|双连通+二分图判定】POJ-2942 Knights of the Round Table
- POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定)
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
- 可伸缩的IO完成端口服务器模型(IOCP)(中文版)
- Hadoop完全分布式配置
- Xcode下cocos2d-x-2.2.2创建HelloWorld
- After searching for a long time,it finally appears ! !
- Navigation Bar 修改问题(五) ------ setSystemUiVisibility接口异常
- POJ 2942 圆桌骑士 点双连通+二分图判定
- 可伸缩的IO完成端口服务器模型(IOCP)(英文版)
- 终端IO--unix环境高级编程读书笔记
- 经典算法实现(1):猴子选大王
- Navigation Bar 修改问题(六)------ Recents启动应用Frame异常和Navbar闪烁
- Jersey+Spring构建RESTful Web服务
- 黑马程序员_基础知识(3)
- discuz7.0端口号留存修改工作
- STL 中 sort 函数用法简介