UVALive
来源:互联网 发布:数据库王珊第五版答案 编辑:程序博客网 时间:2024/05/30 23:18
题目分析
这道题是白书上的一道原题,但是思路很巧妙,如果没看分析我做不出来。这里我说一下自己的理解。因为互相讨厌的其实不能坐在圆桌上的相邻位置,那么很明显我们可以在不互相憎恨的骑士中间建立边,这样就转化为了求不在任何一个奇圈上的结点个数。
奇圈上的所有结点必然属于同一个双联通分量,因此第一步是找双联通分量。又因为二分图没有奇圈,因此我们只需要关注不是二部图的双联通分量。 又因为不是二部图所以必定含有一个奇圈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;}
0 0
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- URL详解
- 字符串比较函数
- Uva1594 Ducci Sequence
- 用通俗易懂的大白话讲解Map/Reduce原理
- 2291 糖果堆
- UVALive
- JAVA Swing JButton按钮的事件响应
- HDU2096 小明A+B
- 廖雪峰老师——Python进阶( 定制类 )
- 带参模糊查询
- BZOJ P2330[SCOI2011]糖果
- 无等待地从邮箱中得到一个消息, OSMboxAccept()
- PCIE M.2规格书之2.3.4 板卡PCB详解
- 中介者模式