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, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
- The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
- An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)
Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled.
Input
The input is terminated by a block with n = m = 0 .
Output
Sample Input
5 5
1 4
1 5
2 5
3 4
4 5
0 0
Sample Output
2
Hint
题目大意 有n个骑士和m个对憎恨关系,每次圆桌会议至少3人参加且人数必须为奇数,互相憎恨的骑士不能邻座。问有多少骑士不可能参加任何一场圆桌会议。
首先直接按照憎恨关系建图总之我做不出来。所以考虑补图,补图的意义是可以邻座的关系建出来的图(正难则反)。那么一个骑士可以参加某场圆桌会议就等价于存在一个长度为奇数的简单环包含它。
所以可以考虑把所有点双连通分量求出来,然后dfs染色进行判断。如果一个点-双联通分量中存在一个奇环,那么这个点-双连通分量内的所有奇环覆盖了这中间所有点。
假设存在一个奇环,那么考虑任意一个不在这个奇环内的点$p$,以及任意一个奇环上的点$q$。由点双性质有,存在两条公共点只有$p, q$的路径使得从$p$到达$q$。设两条路径到达环的第一个点分别为$u, v$。显然存在一种方案使得$u\neq v$,否则可以得到$u$是一个割点。$u, v$间在环上有两条路径$l_1, l_2$,显然$2\nmid |l_1| - |l_2|$。考虑$(p \rightarrow u) - l_1 - (v \rightarrow p)$和$(p \rightarrow u) - l_2 - (v \rightarrow p)$这两个环,它们环长的奇偶性显然不同。所以存在一个奇环包含$p$。
所以如果一个点双连通分量不能够被染色,就可以把它包含的所有点标为可以参加某场会议。
Code
/**
* poj
* Problem#2942
* Accepted
* Time: 1141ms
* Memory: 1232k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef bool boolean;
#define smin(a, b) a = min(a, b) ///map template starts
typedef class Edge{
public:
int end;
int next;
int w; Edge(const int end = , const int next = -, const int w = ):end(end), next(next), w(w) { }
}Edge; typedef class MapManager{
public:
int ce;
int *h;
vector<Edge> edge; MapManager() { }
MapManager(int points):ce() {
h = new int[(const int)(points + )];
memset(h, -, sizeof(int) * (points + ));
} inline void addEdge(int from, int end, int w) {
edge.push_back(Edge(end, h[from], w));
h[from] = ce++;
} inline void addDoubleEdge(int from, int end, int w) {
addEdge(from, end, w);
addEdge(end, from, w);
} inline void clear() {
delete[] h;
edge.clear();
} Edge& operator [] (int pos) {
return edge[pos];
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
#define m_endpos -1
///map template ends int n, m;
boolean hated[][];
MapManager g; inline boolean init() {
scanf("%d%d", &n, &m);
if(!n && !m) return false;
memset(hated[], false, sizeof(hated[]) * n);
g = MapManager(n);
for(int i = , u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
hated[u][v] = hated[v][u] = true;
}
return true;
} int cnt = ;
int cc = ;
stack<int> s;
int visitID[];
int exitID[];
boolean visited[];
int counter[];
vector< vector<int> > contains;
inline void init_tarjan() {
for(int i = ; i <= n; i++)
for(int j = i + ; j <= n; j++)
if(i != j && !hated[i][j])
g.addDoubleEdge(i, j, ); cnt = , cc = ;
memset(visited, false, sizeof(boolean) * (n + ));
} void tarjan(int node, int fae) {
visitID[node] = exitID[node] = ++cnt;
visited[node] = true; for(int i = m_begin(g, node); i != m_endpos; i = g[i].next) {
if(i == fae) continue;
int& e = g[i].end;
if(!visited[e]) {
s.push(i);
tarjan(e, i ^ );
smin(exitID[node], exitID[e]);
if(exitID[e] >= visitID[node]) {
int j = ;
cc++;
vector<int> l;
do {
j = s.top();
s.pop();
g[j].w = g[j ^ ].w = cc;
l.push_back(j);
l.push_back(j ^ );
// printf("%d %d %d\n", g[j ^ 1].end, g[j].end, cc);
} while (j != i);
contains.push_back(l);
}
} else {
smin(exitID[node], visitID[e]);
if(visitID[e] < visitID[node])
s.push(i);
}
}
} int color[];
boolean dfs(int node, int limit, int c) {
if(color[node] != -) return color[node] == c;
color[node] = c;
for(int i = m_begin(g, node); i != m_endpos; i = g[i].next) {
if(g[i].w != limit) continue;
int& e = g[i].end;
if(!dfs(e, limit, c ^ )) return false;
}
return true;
} int res;
boolean aced[];
inline void solve() {
res = ;
memset(aced, false, sizeof(int) * (n + ));
// cout << cc << endl;
for(int c = ; c < (signed)contains.size(); c++) {
memset(color, -, sizeof(int) * (n + ));
boolean aFlag = dfs(g[contains[c][]].end, c + , );
if(!aFlag)
for(int i = ; i < (signed)contains[c].size(); i++)
aced[g[contains[c][i]].end] = true;
}
for(int i = ; i <= n; i++)
if(!aced[i])
res++;
printf("%d\n", res);
} inline void clear() {
g.clear();
contains.clear();
} int main() {
while(init()) {
init_tarjan();
for(int i = ; i <= n; i++)
if(!visited[i])
tarjan(i, -);
solve();
clear();
}
return ;
}
poj 2942 Knights of the Round Table - Tarjan的更多相关文章
- POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
- 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: 10911 Acce ...
- 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求点双联通分量+二分图染色+debug
题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...
- 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 (双连通)
简要题解: 意在判断哪些点在一个图的 奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...
- POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)
题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...
随机推荐
- Selenium基础知识(九)验证码
关于Selenium处理验证码总结下: 1.去掉验证码(这个为了测试,去掉可能性不大) 2.万能验证码(让开发给做一个万能验证码,可能性也不大) 3.OCR光学识别,python包Python-tes ...
- 原生js---ajax---post方法传数据
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- win10系统进入BIOS
按住shift+重启,在重启过程中界面会出现“疑难解答”,点击后,在新的界面点击“高级选项”,之后在新界面上点击“UEFI固件设置”,最后点击重启,重启过程中点击Delete键,就进入了BIOS界面了 ...
- html5-css动画-2d
div{ width: 300px; height: 100px; margin: 50px; padding: 50px; background: green; ...
- Introduction to debugging neural networks
http://russellsstewart.com/notes/0.html The following advice is targeted at beginners to neural netw ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- [转] LoadRunner 获取接口请求响应信息
Action() { int nHttpRetCode; // 默认最大长度为256,get请求需注意缓存问题,需要根据content-length进行修改 web_set_max_html_para ...
- 关于git上的一些错误信息
如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...
- 【Hadoop学习之十】MapReduce案例分析二-好友推荐
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 最应该推荐的好友TopN,如何排名 ...
- Linux基础命令---chfn
chfn chfn指令可以改变通过finger指令查看到的信息.此信息存储在/etc/passwd文件中,并由Finger程序显示.LinuxFinger命令将显示可由chfn更改的四条信息:您的真名 ...