【poj2942】 Knights of the Round Table
http://poj.org/problem?id=2942 (题目链接)
题意
有n个骑士要去参加圆桌会议,他们将围成一圈,想要他们不打架,当且仅当参加圆桌会议的骑士数为奇数并且相邻的两个骑士不互相憎恨。现在给出m条骑士之间两两憎恨的关系,问有多少骑士无论在何种情况下都不能参加圆桌会议。
Solution
思路到是很简单,先构出原图的补图,补图中每条边代表这两个骑士可以相邻。那么很显然,如果某一个骑士处于任意一个奇环中,那么他就可以参加会议。
这个问题该怎么处理呢?我们用Tarjan求出点-双连通分量,对于每一个点双,通过黑白染色看是否存在奇环,若染色不成功,那么存在奇环,否则是偶环。
细节
Tarjan真是恶心爆。。求点-双一定要存边,这样可以避免很多分类讨论。。。
代码
// bzoj1013
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define inf 2147483640
#define eps 1e-7
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
inline int gi() {
int x=0,f=1;char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=1010,maxm=1000010;
struct edge {int to,next;}e[maxm<<1];
struct data {int u,v;}s[maxn];
int vis[maxn],dfn[maxn],low[maxn],f[maxn][maxn],pos[maxn];
int ind,cnt,top,n,m,num,p,id[maxn],head[maxn];
vector<int> v[maxn]; void link(int u,int v) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
}
void Init() {
for (int i=1;i<=num;i++) v[i].clear();
cnt=ind=top=num=0;
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(pos,0,sizeof(pos));
memset(id,0,sizeof(id));
memset(f,0,sizeof(f));
}
void Tarjan(int x,int fa) {
dfn[x]=low[x]=++ind;
for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) {
if (!dfn[e[i].to]) {
s[++top]=(data){x,e[i].to};
Tarjan(e[i].to,x);
low[x]=min(low[x],low[e[i].to]);
if (low[e[i].to]>=dfn[x]) {
num++;
while (1) {
v[num].push_back(s[top].u);v[num].push_back(s[top].v);
if (s[top].u==x && s[top].v==e[i].to) break;
top--;
}
top--;
}
}
else low[x]=min(low[x],dfn[e[i].to]);
}
}
bool dfs(int x,int col) {
id[x]=col;
for (int i=head[x];i;i=e[i].next)
if (pos[e[i].to]==p) {
if (!id[e[i].to]) {
if (!dfs(e[i].to,col^1)) return 0;
}
else if (id[e[i].to]==id[x]) return 0;
}
return 1;
}
void work(int x) {
vis[x]=1;
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to] && pos[e[i].to]==p) work(e[i].to);
}
int main() {
while (1) {
n=gi(),m=gi();
if (!n && !m) break;
Init();
for (int x,y,i=1;i<=m;i++) {
x=gi(),y=gi();
f[x][y]=f[y][x]=1;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) if (i!=j && !f[i][j]) link(i,j);
for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan(i,0);
for (p=1;p<=num;p++) {
for (int j=0;j<v[p].size();j++) pos[v[p][j]]=p;
if (!dfs(v[p][0],p*2)) work(v[p][0]);
}
int ans=0;
for (int i=1;i<=n;i++) if (!vis[i]) ans++;
printf("%d\n",ans);
}
return 0;
}
【poj2942】 Knights of the Round Table的更多相关文章
- 【POJ2942】Knights of the Round Table(二分图 点双联通分量)
题目链接 大意 给定\(N\)个点与\(M\)个关系,每个关系表示某两个点间没有直接的边相连,求不在所有奇环上的点的个数. (\(1\le N\le 1e3,1\le M\le 1e6\)) 思路 考 ...
- 【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(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- [POJ2942][LA3523]Knights of the Round Table
[POJ2942][LA3523]Knights of the Round Table 试题描述 Being a knight is a very attractive career: searchi ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)
圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...
- 【洛谷 SP2878】Knights of the Round Table(双联通分量)
先放这吧,没时间写,明天再补 "明天到了" 题目链接 题意:求不在任何奇环内的点的数量. Tarjan求点双联通分量,然后再染色判断是不是二分图就好了. 只是不懂为什么Tarjan ...
- POJ2942:Knights of the Round Table——题解
http://poj.org/problem?id=2942 所写的tarjan练习题最难的一道. 说白了难在考得不是纯tarjan. 首先我们把仇恨关系处理成非仇恨关系的图,然后找双连通分量,在双连 ...
- 【POJ】2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
随机推荐
- DEDECMS之十 修改织梦链和文章的默认来源及作者
今天在用织梦搭网站的时候,发现了两个问题,一个就是最新的dedecms5.7系统中默认会加上“织梦链”这一个链接组,织梦的做法是可以理解的, 但是给别人做网站,这些链接是不能要的,所以在数据库,模板文 ...
- 阿里云修改默认的ssh端口
Linux服务器的ssh服务支持远程访问服务器,默认的ssh端口号是22.为了安全起见,很多用户会将端口号由22改为其他的端口号. 如果遇到修改端口号并重启ssh服务后,新的端口号不生效,请参考以下 ...
- Cannot resolve external dependency com.android.support:multidex:1.0.0
multiDexEnabled true 去掉这一行,就OK了,是没有多dex支持么,但是又提供了这个
- java 客户端链接不上redis解决方案
原文地址:http://blog.csdn.net/yingxiake/article/details/51472810 出现问题描述: 1.Could not get a resource from ...
- Qt 5.2 Creator 和 vs2012 QT 插件的安装
在QT官网下载QT http://qt-project.org/downloads 我下的是64位版本Qt 5.2.1 for Windows 64-bit vs2012插件是 Visual Stu ...
- 别再迷信 zepto 了
希望网上公开课的老师们不要再讲移动端网页用zepto了,坑了无数鸟啊 ~~~. 1.自己/公司/项目组所写和所积累(网上下的)的js函数都是以jQuery插件的写法来写的,如果要换到zepto上的话那 ...
- SQL 批量修改表结构
项目中发现一批语言表的某个字段设的值太小了需要增大,因为涉及到很多张表,所以采用游标一张张的处理. 代码很简单 ) ) DECLARE LangTable CURSOR FOR SELECT name ...
- Vue学习笔记-2
前言 本文非vue教程,仅为学习vue过程中的个人理解与笔记,有说的不正确的地方欢迎指正讨论 1.computed计算属性函数中不能使用vm变量 在计算属性的函数中,不能使用Vue构造函数返回的vm变 ...
- sql中去除重复的项
方法一:group by (取最小的id)select min(id) id,T from Table_1 group by T 方法二:union (不需要id)select T from Tab ...
- WP8 对音视频格式支持的完整说明
Supported media codecs for Windows Phone http://msdn.microsoft.com/en-us/library/windowsphone/develo ...