POJ 2942.Knights of the Round Table (双连通)
简要题解:
意在判断哪些点在一个图的 奇环的双连通分量内。
tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内
输出没有在奇环内的点的数目
code
/*
求有向图的点双连通分支tarjan算法
思路:
1.对图先进行深度优先搜索形成搜索数,计算每一个节点的先深编号dfn[n]
2.计算所有节点v的low[v]是在先深生成树上按照后根遍历的顺序进行的.
因此,当仿问节点v时它的每一个儿子u的low[u]已经计算完毕这时low[v]取下面三值的最小者:
1)dfn[v];
2)dfn[w],对于回退边(v,w)
3)low[u],对于v的任何儿子u
3.判断一个顶点是不是桥,割点:
a)v为树根,且v有多于1个子树
b)v不为树根,且满足存在边(v,u) ,使得dfn[v]<=low[u].
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 1009
using namespace std;
int n, m, x, y;
bool g[INF][INF];
int low[INF], dfn[INF], sta[INF], ans[INF][INF], f[INF], ok[INF], Top, Max , tcc, t;
bool make (int x, int cow) {
for (int i = 0; i < cow; i++) {
int v = ans[tcc][i];
if (x != v && g[x][v]) {
if (f[v] == -1) {
f[v] = !f[x];
if (make (v, cow) ) return 1;
}
else if (f[v] == f[x]) return 1;
}
}
return 0;
}
void check (int cow) {
memset (f, -1, sizeof f);
f[ans[tcc][0]] = 0;
if (make (ans[tcc][0], cow) )
for (int i = 0; i < cow; i++)
ok[ans[tcc][i]] = 1;
}
void dfs (int k, int from) {
sta[++Top] = k;
low[k] = dfn[k] = ++t;
for (int i = 1; i <= n; i++) {
if (i == from || g[k][i] == 0 || k == i) continue;
if (!dfn[i]) {
dfs (i, k);
low[k] = min (low[k], low[i]);
if (dfn[k] <= low[i]) {
ans[tcc][0] = k;
int cow = 1;
do
ans[tcc][cow++] = sta[Top];
while (sta[Top--] != i);
if (cow > 2) check (cow), ++tcc;
}
}
else low[k] = min (low[k], dfn[i]);
}
return ;
}
void Tarjan (int n) {
memset (low, 0, sizeof low);
memset (dfn, 0, sizeof dfn);
Top = tcc = t = 0;
for (int i = 1; i <= n; i++)
if (dfn[i] == 0) dfs (i, -1);
}
int main() {
while (~scanf ("%d %d", &n, &m) ) {
if (n == 0 && m == 0) return 0;
memset (g, 1, sizeof g);
memset (ok, 0, sizeof ok);
Max = 0;
for (int i = 1; i <= m; i++) {
scanf ("%d %d", &x, &y);
g[x][y] = g[y][x] = 0;
}
Tarjan (n);
int ans = 0;
for (int i = 1; i <= n; i++)
if (!ok[i]) ans++;
printf ("%d\n", ans);
}
return 0;
}
POJ 2942.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 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- 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 (点双连通分量)
题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...
- poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
- POJ 2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
- POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)
题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...
随机推荐
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- OpenStack学习推荐
前言: 学习.了解.安装部署OpenStack也写了好一段时间了,现在觉得也有点必要写点总结.搞IT这一块基本就这样,不管是搞什么领域,一个项目超过两个月没动它,就基本不知道当时是怎么搞的,当时学 ...
- 从物理执行的角度透视spark Job
本博文主要内容: 1.再次思考pipeline 2.窄依赖物理执行内幕 3.宽依赖物理执行内幕 4.Job提交流程 一:再次思考pipeline 即使采用pipeline的方式,函数f对依赖的RDD中 ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- Docker官方文档翻译之入门
转自:http://www.cnblogs.com/vikings-blog/p/3958091.html Docker学习总结之docker入门 Understanding Docker 以下均翻译 ...
- Qt对话框QDialog
QDialog是Qt中所有对话框窗口的基类 当QWidget无父组件的时候作为一个独立的窗口,有父组件的时候,将作为一个可见的部件嵌入到父组件里面. QDialog不能作为子部件嵌入到其他容器中 对话 ...
- Java生成缩略图Thumbnailator(转载)
转自(http://rensanning.iteye.com/blog/1545708) Thumbnailator 是一个为Java界面更流畅的缩略图生成库.从API提供现有的图像文件和图像对象的缩 ...
- 教你Mac OS系统四种改动Hosts文件的方法
使用Mac OS X系统的用户.在某些时候可能遇到了须要改动系统Hosts文件的情况,那么Mac OS系统怎样改动Hosts文件呢?和Windows系统有何差别呢?我们知道事实上改动Hosts文件仅仅 ...
- [Redux] Persisting the State to the Local Storage
We will learn how to use store.subscribe() to efficiently persist some of the app’s state to localSt ...
- nginx代理人server结合tomcat采用
相信非常多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那到底它有什么作用呢?可能非常多人未必了解. 说到反向代理,可能非常多人都听说,但详细什么是反向代理,非常多人预计就 ...