UVALive - 3523 Knights of the Round Table(双联通分量)
2017-04-05 20:01
507 查看
题目分析
这道题是白书上的一道原题,但是思路很巧妙,如果没看分析我做不出来。这里我说一下自己的理解。因为互相讨厌的其实不能坐在圆桌上的相邻位置,那么很明显我们可以在不互相憎恨的骑士中间建立边,这样就转化为了求不在任何一个奇圈上的结点个数。奇圈上的所有结点必然属于同一个双联通分量,因此第一步是找双联通分量。又因为二分图没有奇圈,因此我们只需要关注不是二部图的双联通分量。 又因为不是二部图所以必定含有一个奇圈C,那么对于其他顶点来说必定和C存在2个相接的点(因为这是个双联通分量),那么对于奇圈来说这2个相接的点把奇圈分为2部分,必然是一个是奇,一个是偶,那么这样就可以保证该双联通上的所有点都存在于奇环上。
#include <stack> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1005; int pre[maxn], iscnt[maxn], bccno[maxn], dfs_clock, bcc_cnt; vector <int> G[maxn], bcc[maxn]; int odd[maxn], color[maxn], A[maxn][maxn]; struct Edge{ int u, v; }; stack <Edge> S; 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 = (Edge){u, v}; if(!pre[v]){ S.push(e); child++; int lowv = dfs(v, u); lowu = min(lowu, lowv); if(lowv >= pre[u]){ iscnt[u] = true; bcc_cnt++; bcc[bcc_cnt].clear(); for(;;){ Edge x = S.top(); S.pop(); if(bccno[x.u] != bcc_cnt){ bcc[bcc_cnt].push_back(x.u); bccno[x.u] = bcc_cnt; } if(bccno[x.v] != bcc_cnt){ bcc[bcc_cnt].push_back(x.v); bccno[x.v] = bcc_cnt; } if(x.u == u && x.v == v) break; } } } else if(pre[v] < pre[u] && v != fa){ S.push(e); lowu = min(lowu, pre[v]); } } if(fa < 0 && child == 1) iscnt[u] = 0; return lowu; } bool bipartite(int u, int b){ for(int i = 0; i < G[u].size(); i++){ int v = G[u][i]; if(bccno[u] != 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 find_bcc(int n){ memset(pre, 0, sizeof(pre)); memset(iscnt, 0, sizeof(iscnt)); memset(bccno, 0, sizeof(bccno)); dfs_clock = bcc_cnt = 0; for(int i = 0; i < n; i++){ if(!pre[i]) dfs(i, -1); } } int main(){ int n, m; while(scanf("%d%d", &n, &m) != EOF && n){ for(int i = 0; i < n; i++) G[i].clear(); memset(A, 0, sizeof(A)); for(int i = 0; i < m; i++){ int u, v; scanf("%d%d", &u, &v); u--, v--; A[u][v] = A[v][u] = 1; } for(int u = 0; u < n; u++) for(int v = u+1; v < n; v++) if(!A[u][v]){ G[u].push_back(v); G[v].push_back(u); } find_bcc(n); memset(odd, 0, sizeof(odd)); 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 = 0; i < n; i++) if(odd[i]) ans--; printf("%d\n", ans); } return 0; }
相关文章推荐
- UVALive3523 [Knights of the Round Table] tarjan求无向图双联通分量
- UVALive 3523 Knights of the Round Table(BCC+二分图)
- UVALive - 3523 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- uva1364 - Knights of the Round Table 点-双联通分量
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive 3523 Knights of the Round Table(二分图+双连通分量)
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
- UVAlive 3523 Knights of the Round Table [点双连通分量] [Tarjan]
- UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
- UVALive3523-Knights of the Round Table(BCC+二分图判定)
- UVALive - 3523 Knights of the Round Table(无向图的双连通分量)
- UVAlive3523 Knights of the Round Table(bcc)
- POJ 2942 Knights of the Round Table 点双联通分量
- poj 2942 Knights of the Round Table 点重联通分量
- [省选前题目整理][POJ 2942]Knights of the Round Table(Tarjan求点双联通分量+DFS对环染色)
- POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug
- POJ 2942:Knights of the Round Table tarjan点双联通分量 二分图染色找奇环
- poj 2942 Knights of the Round Table Tarjan求点双联通分量+黑白染色二分图判断
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)