La3523 Knights of the round table
来源:互联网 发布:淘宝粉丝红包 编辑:程序博客网 时间:2024/06/06 07:41
题目大意:
给你n个人和m组关系,每组关系表示两个人相互憎恨,而且相互憎恨的人不能在参加一场会议相邻着坐,而且每次会议参加的人数必须为奇数,问最多有多少人不能同时参加一场会议。
分析:
对于每一个人而言,他两边坐的人只能是与他不相互憎恨的,所以我们可以把不相互憎恨的两个人之间连一条边,那么每一次参加会议的人就必须在同一个双连通分量上,这样才能形成过一个环形图,关键是如何判断这个环是不是一个奇环,那么最重要的问题转移到了如何判断一个连通图是不是奇环上来了,根据二分图的定义,我们知道如果一个环是二分图,那么这个环必定是偶环。(证明:由染色法,一个二分图上的某个点与它相邻的点之间必定不属于一个子图,则可以将它们用不同的颜色染上来区分它们,这种方法同样适用于二分图的判定,而对于一个奇环来说,染色的最终结果必定使得两个相邻点的颜色相同,所以奇环不可能是二分图。)
代码:
#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<stack>using namespace std;const int maxn = 1000 + 10;const int maxm = 1000000 + 10;struct Edge { int u, v;};int Begin[maxn], Next[maxm*2], To[maxm*2], E;void Add(int x, int y) { To[++E] = y; Next[E] = Begin[x]; Begin[x] = E;}bool odd[maxn];int A[maxn][maxn], color[maxn];int pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;vector<int> bcc[maxn];stack<Edge> S;int dfs(int u, int fa) { int child = 0; int lowu = pre[u] = ++dfs_clock; for(int i=Begin[u]; i; i=Next[i]) { int v = To[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]) { iscut[u] = 1; bcc[++bcc_cnt].clear(); while(1) { Edge x = S.top(); S.pop(); if(bccno[x.u] != bcc_cnt) bccno[x.u] = bcc_cnt, bcc[bcc_cnt].push_back(x.u); if(bccno[x.v] != bcc_cnt) bccno[x.v] = bcc_cnt, bcc[bcc_cnt].push_back(x.v); 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) 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=1; i<=n; i++) if(!pre[i]) dfs(i, -1);}bool bipartite(int u, int v) { for(int i=Begin[u]; i; i=Next[i]) { int t = To[i]; if(bccno[t] != v) continue; if(color[u] == color[t]) return false; if(!color[t]) { color[t] = 3 - color[u]; if(!bipartite(t, v)) return false; } } return true;}void init() { E = 0; memset(A, 0, sizeof(A)); memset(odd, 0, sizeof(odd)); memset(Begin, 0, sizeof(Begin));}int n, m, u, v;int main() { while(scanf("%d%d", &n, &m) == 2 && n+m) { init(); for(int i=1; i<=m; i++) { scanf("%d%d", &u, &v); A[u][v] = A[v][u] = 1; } for(int u=1; u<=n; u++) for(int v=u+1; v<=n; v++) if(!A[u][v]) { Add(u, v); Add(v, u); } 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; }
0 0
- LA3523 Knights of the Round Table
- La3523 Knights of the round table
- UVA-Knights of the Round Table La3523
- [双连通分量]LA3523 Knights of the Round Table
- LA3523.Knights of the Round Table圆桌骑士——点双连通分量+二分图判断
- Knights of the Round Table
- Knights of the Round Table
- Knights of the Round Table
- The Knights of the Round Table
- POJ 2942 Knights of the Round Table
- poj 2942 Knights of the Round Table
- POJ 2942 Knights of the Round Table
- POJ 2492 Knights of the Round Table
- UVA1364 Knights of the Round Table
- POJ2942--Knights of the Round Table
- POJ 2942: Knights of the Round Table
- LA 3523 - Knights of the Round Table
- poj 2942 Knights of the Round Table
- 数据分析实例--兰州二手房数据分析(一)
- 火爆朋友圈的柏拉图性格标签生成器源码下载
- Android基础
- Android UI设计(引导界面):ViewPager之三PagerTabStrip与PagerTitleStrip添加标题栏的区别
- mybatis笔记
- La3523 Knights of the round table
- Java内存回收
- MAC上安装使用KYLIN(OLAP工具)完整版
- 关于C++ 顶层const和底层const对拷贝操作的影响
- [Codeforces 697C] PLEASE (矩阵特征向量)
- 寻找【NOIP2016提高A组模拟7.17】
- usaco 2.2.2 subset
- hadoop平台安装搭建
- Objective-c中.m、.h、.mm文件