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条关系是讨厌,将每个人视作点 ...
随机推荐
- VUE中过了一遍还不熟悉的东西
1.computed/watch/和methods computed是依赖于数据来变动的,有缓存,当不需要缓存的时候就用方法,watch不建议乱用,当有异步请求的时候就用watch 写法一样 2.wa ...
- Css预处理器---Less(二)
三.Less语法 (1)变量 //less代码 @nice-blue : #5B83AD; @light-blue : @nice-blue + #111; #header { color : @li ...
- UGUI 打图集
using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; us ...
- IIS 8.0 Using ASP.NET 3.5 and ASP.NET 4.5微软官方安装指导
from:https://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-using-aspnet-35-and-aspnet-45 S ...
- kafka 单机配置
http://blog.csdn.net/jingshuigg/article/details/24439637 . su root : chown -R hadoop:hadoop version- ...
- tcl脚本
tcl,全名tool command language,是一种通用的工具语言. 1)每个命令之间,通过换行符或者分号隔开: 2)tcl的每个命令包含一个或者多个单词,默认第一个单词表示命令,第二个单词 ...
- 06 str() bytes() 编码转换
x = str() #创建字符串#转换成字符串,字节,编码 m = bytes()#创建字节#转换成字节,字符串,要编程什么编码类型的字节 a = "李露" b1 = bytes( ...
- hdu2609最小表示法
#include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...
- mysql 安装目录说明
- numpy高级应用
reshape重塑数组 ravel 拉平数组 concatenate 最一般化的连接,沿一条轴连接一组数组 # print(np.concatenate([arr1,arr2],axis = 0)) ...