poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std; const int maxn = 1000 + 10;
//const int INF = 0x3f3f3f3f;
int n, m;
int A[maxn][maxn]; //计算点—双连通分量。用栈S来保留当前bcc中的边
int pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;
vector<int> G[maxn], bcc[maxn];
struct Edge {
int u, v;
Edge(int u = 0, int v = 0) : u(u), v(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]) {
iscut[u] = true;
bcc_cnt++; bcc[bcc_cnt].clear(); //注意! bcc从1開始编号
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) 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);
}
} //dfs给二分图进行黑白二着色,用颜色1表示黑色,颜色2表示白色,0表示没着色
int color[maxn]; //推断节点u所在的连通分量是否为二分图
bool bipartite(int u, int id) {
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if(bccno[v] != id) continue;
if(color[v] == color[u]) return false;
if(!color[v]) {
color[v] = 3 - color[u];
if(!bipartite(v, id)) return false;
}
}
return true;
} int odd[maxn];
void init() {
int u, v;
memset(A, 0, sizeof(A));
memset(odd, 0, sizeof(odd));
while(m--) {
scanf("%d%d", &u, &v);
u--; v--;
A[u][v] = A[v][u] = 1;
}
for(int i = 0; i < n; i++) G[i].clear();
for(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);
} void solve() {
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 = 0;
for(int i = 0; i < n; i++) if(!odd[i])
ans++;
cout << ans << endl;
} int main() {
//freopen("input.txt", "r", stdin);
while(scanf("%d%d", &n, &m) == 2 && n) {
init();
solve();
}
return 0;
}
题意:有n个骑士开会,每次至少三个人且人数必须为奇数,相互憎恨的人不能相邻,给出骑士们相互的憎恨关系。求多少骑士不能參加不论什么一个会议。
大白书经典例题。写完以后感觉收获非常大。
1.以骑士为节点建立无向图,假设两个骑士不憎恨,那么在他们之间连一条边,则题目转化为求不在任一简单奇圈上的结点个数。
2.简单圈上的全部节点必定属于同一双连通分量,因此须要先找出全部双连通分量。
3.非常重要的一个结论!。!!。!
!!对于一个双连通分量来说,假设他是二分图,那么是没有奇圈的。反之有奇圈的图一定不是二分图,证明略。
4.也非常重要的一个结论。!。!!!
。假设结点v所属的一个双连通分量不是二分图。那么v一定属于一个奇圈。如今证明这个结论,假设双连通分量B不是一个二分图,依据3,B中一定含有一个奇圈,那么对于不属于这个奇圈的v来说。依据双连通性,一定存在两条不相交路径(除起点外无公共结点),使得v能到达这个奇圈上的两个不同点,设为v1。v2.
而由于v1和v2在一个不同的奇圈上,那么从v1到v2的两条路径长度一奇一偶。所以总能构造出一条经过v的奇圈。
5.注意。每一个割顶属于多个双连通分量。可能被标记多次。
poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)的更多相关文章
- POJ 2942 Knights of the Round Table (点双连通分量)
题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...
- POJ2942 Knights of the Round Table(点双连通分量 + 二分图染色)
题目大概说要让n个骑士坐成一圈,这一圈的人数要是奇数且大于2,此外有些骑士之间有仇恨不能坐在一起,问有多少个骑士不能入座. 双连通图上任意两点间都有两条不重复点的路径,即一个环.那么,把骑士看做点,相 ...
- POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
- poj 2942 Knights of the Round Table - Tarjan
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- POJ 2942 Knights of the Round Table - from lanshui_Yang
Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
随机推荐
- 1.7(学习笔记)过滤器(Fliter)
一.过滤器(Fliter)简介 过滤器是位于客户端与服务器之间的滤网,在访问资源时会经过一系列的过滤器, 满足条件则放行,不满足条件的将其拦截. 过滤器可以看做是一个特殊的Servlet,设置了过滤器 ...
- 编译boost到各个系统平台 mac,iOS,linux,android,wind
编译boost到各个系统平台 mac,iOS,linux,android,wind git地址:https://github.com/czjone/boost git仓库:https://github ...
- js常用功能总结
1,手机号的校验 //手机号的判断 function checktel() { //手机号不为空,格式校验 var tel = $(".uidbtp").val(); if(tel ...
- cookie的secure、httponly属性设置
cookie的secure.httponly属性设置 转载自:http://www.cnblogs.com/alanzyy/archive/2011/10/14/2212484.html 一.属性说明 ...
- 64945e3dtw1dii6vfdr19j.jpg(PNG 图像,1497x929 像素)
64945e3dtw1dii6vfdr19j.jpg(PNG 图像,1497x929 像素)
- Debian6 安装Kscope(也适用于Ubuntu)
参考:http://soft.chinabyte.com/os/134/12307634.shtml kscope1.6.2在这里下载,下载后解压出kscope-1.6.2.tar.gz. 在ubun ...
- Array.apply 方法的使用
Array.apply(null, {length: 5}) length为特殊字段,意思是生成一个长度为5的数组,由于没赋值,所以都是undefined; 如果要赋值,可以这样 console.lo ...
- CentOS6.6下DRBD+HeartBeat+NFS配置
一.DRBD配置 Distributed Replicated Block Device(DRBD)是一个用软件实现的.无共享的.服务器之间镜像块设备内容的存储复制解决方案. 我们可以理解为它其实就是 ...
- ISP图像调试工程师——边缘增强(熟悉图像预处理和后处理技术)
http://blog.csdn.net/u013033431/article/details/50907907 http://dsqiu.iteye.com/blog/1638589 概念: 图像增 ...
- 【CloudFoundry】架构、设计参考
参考资料: Cloud Foundry:http://baike.baidu.com/link?url=eIfPiUI8UlsqwnnSmmZ-WFyzrf38P33lJae4Hipsd0ynwXZp ...