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条关系是讨厌,将每个人视作点 ...
随机推荐
- 实现多线程异步自动上传本地文件到 Amazon S3
最近抽空做个小工具,使用AWSSDK 对本地文件目录监控,并自动同步上传文件到S3 的过程,使用的是多线程异步上传,针对大文件进行了分块 参考文献: https://www.codeproject.c ...
- dubbo.provider和dubbo.consumer配置
Configure service provider <?xml version="1.0" encoding="UTF-8"?> <bean ...
- HttpClient超时设置setConnectionTimeout和setSoTimeout
http是基于TCP/IP进行通信的,tcp通过3次握手建立连接,并最终以4次挥手终止通信. 知乎上对三次握手和四次挥手有如下解释: 作者:知乎用户链接:https://www.zhihu.com/q ...
- H5进行录音,播放,上传
废话不说,直接上代码吧 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
- repr()函数
http://www.cnblogs.com/itdyb/p/5046415.html
- sitecore系统教程之限制对客户端的访问
如果您为不同目的配置服务器,根据角色,您可能需要禁用Sitecore客户端.例如,如果配置内容交付服务器或处理服务器,则无需访问客户端应用程序,因此在这种情况下,建议禁用客户端. 为防止未经授权访问S ...
- 20155228 基于VirtualBox安装Ubuntu和学习linux命令的学习经历和心得
一.虚拟机VirtualBox的下载安装 基于VirtualBox虚拟机安装Ubuntu图文教程 虽然娄老师的教程对于VirtualBox的下载安装讲的很简单,可以说是一笔带过,但是我在下载安装的过程 ...
- MySql 存储过程 光标只循环一次
[1]MqSql 存储过程 光标只循环一次 针对MySql存储过程,光标只循环一次就退出的场景,可能原因分析: (1)存储过程有问题(仔细检查语法.控制变量.条件等等) (2)保证存储过程正确.调用过 ...
- 数据迁移工具Sqoop和DataX功能比较
本文转载自: http://www.cnblogs.com/panfeng412/archive/2013/04/29/data-migration-tool-sqoop-and-datax.html ...
- mybatis源码解析9---执行器Executor解析
从前面分析我们知道了sql的具体执行是通过调用SqlSession接口的对应的方法去执行的,而SqlSession最终都是通过调用了自己的Executor对象的query和update去执行的.本文就 ...