【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. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
随机推荐
- JS常用自定义方法
1,JS生成随机数方法 getRandom(100),表示生成0-100的数 function getRandom(n){ return Math.floor(Math.random()*n+1) } ...
- 根据Unicode编码用C#语言把它转换成汉字的代码
rt 根据所具有的Unicode编码用C#语言把它转换成汉字的代码 var s = System.Web.HttpUtility.HtmlDecode(Utf8Str); var o = Newton ...
- Post model至Web Api
Post model可以解决多动态性的参数至Web Api中去,获取数据或是创建数据.如果一个对象有很多字段,需要对每一个字段进行查询或是在创建对象时,我们可以考虑使用model来作参数.这样不必在控 ...
- Linux时间函数之gettimeofday()函数之使用方法
1.简介: 在C语言中可以使用函数gettimeofday()函数来得到时间.它的精度可以达到微妙 2.函数原型: #include<sys/time.h> int gettimeofda ...
- IE8中给HTML标签负值报错问题
当通过JS给一个HTML标签设置高宽为负值的时候,会爆出一个“参数无效”的错误 chrome下不会报错,但是元素不会做任何关于负值的改变
- Android酷炫实用的开源框架(UI框架)
Android酷炫实用的开源框架(UI框架) 前言 忙碌的工作终于可以停息一段时间了,最近突然有一个想法,就是自己写一个app,所以找了一些合适开源控件,这样更加省时,再此分享给大家,希望能对大家有帮 ...
- Linux下C语言编程实现spwd函数
Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...
- Linux学习笔记-Ubuntu添加右键菜单打开终端
1.进入个人目录(如/home/batsing,下文缩写成 ~ ):设置显示隐藏文件,或使用命令行:2.进入 ~/.gnome2/nautilus-scripts 文件夹,新建一个文件,名为 term ...
- C#微信开发小白成长教程二(新手接入指南,附视频)
距离第一讲又已经过去了一个多星期了,本打算一周更新一讲的,奈何实在太忙.最近也在群里发现有一部分人已经可以熟练调用微信的部分接口但却不是很清楚微信公众平台接收消息的一个处理机制.本讲就来介绍下怎么接入 ...
- unity3d Vector3.Lerp解析
Vector3.Lerp:http://www.ceeger.com/Script/Vector3/Vector3.Lerp.html 手册中描述的不是很详细,什么叫“按照数字t在from到to之间插 ...