【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
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 contains several blocks of test cases. Each case begins with a line containing two integers
1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000. The number n is the number of knights. The next m lines describe
which knight hates which knight. Each of these m lines contains two integers k1 and k2, which means
that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1
and n).
The input is terminated by a block with n = m = 0.
Output
For each test case you have to output a single integer on a separate line: the number of knights that
have to be expelled.
Sample Input
5 5
1 4
1 5
2 5
3 4
4 5
0 0
Sample Output
2
【题意】
给你n个人和m组关系,每组关系表示两个人相互憎恨,而且相互憎恨的人不能在参加一场会议相邻着坐,而且每次会议参加的人数必须为奇数,问最多有多少人不能同时参加一场会议。
【分析】
为什么我没有做圆桌骑士?为什么我没有做圆桌骑士?为什么我没有做圆桌骑士?
记得以前明明做过嘛- -啊- -怎么找不到带代码,晕..
再做一次啊。
其实如果不是一道经典题,还是很难的(像是我这样子的水平,可能建图都想不到ORZ)
可以把不相互憎恨的两个人之间连一条边,那么每一次参加会议的人就必须在同一个双连通分量上,这样才能形成过一个环形图,关键是如何判断这个环是不是一个奇环,根据二分图的定义,我们知道如果一个环是二分图,那么这个环必定是偶环。
还有一个定理:若某个点双连通分量中存在奇环,则该点双联通分量中所有点都在某个奇环内。(这个东东画个图想想就好了,想想点双连通的性质,奇数=偶数+奇数)
判奇环用厉害的染色法。。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define Maxn 1010 bool a[Maxn][Maxn]; struct node
{
int x,y,next;
}t[Maxn];int len;
int first[Maxn]; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;
t[len].next=first[x];first[x]=len;
} int dfn[Maxn],low[Maxn]; stack<int > s;
vector<int > v[Maxn];
int vl,cnt;
int col[Maxn];
bool q[Maxn]; void ffind(int x,int f)
{
dfn[x]=low[x]=++cnt;
s.push(x);
for(int i=first[x];i;i=t[i].next) if(t[i].y!=f)
{
int y=t[i].y;
if(!dfn[y])
{
ffind(y,x);
low[x]=mymin(low[x],low[y]);
if(low[y]>=dfn[x])
{
vl++;
// memset(v[vl],0,sizeof(v[vl]));
v[vl].clear();
while()
{
int z=s.top();
v[vl].push_back(z);
if(z==x) break;
s.pop();
}
}
}
else low[x]=mymin(low[x],dfn[y]);
}
} bool dfs(int x)
{
for(int i=first[x];i;i=t[i].next) if(col[t[i].y]!=-)
{
int y=t[i].y;
if(col[y]!=-&&col[y]==col[x]) return ;
else if(col[y]==-)
{
col[y]=-col[x];
if(!dfs(y)) return ;
}
}
return ;
} int main()
{
int n,m;
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==) break;
memset(a,,sizeof(a));
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[x][y]=a[y][x]=;
}
len=;vl=;cnt=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++) if(a[i][j])
{
ins(i,j);ins(j,i);
}
memset(dfn,,sizeof(dfn));
while(!s.empty()) s.pop();
for(int i=;i<=n;i++) if(!dfn[i])
{
ffind(i,);
}
for(int i=;i<=n;i++) col[i]=-;
memset(q,,sizeof(q));
for(int i=;i<=vl;i++)
{
for(int j=;j<v[i].size();j++) col[v[i][j]]=-;
col[v[i][]]=;
int x=v[i][];
if(!dfs(x))
{
for(int j=;j<v[i].size();j++) q[v[i][j]]=;
}
for(int j=;j<v[i].size();j++) col[v[i][j]]=-;
}
int ans=;
for(int i=;i<=n;i++) if(q[i]) ans++;
printf("%d\n",ans);
}
return ;
}
[LA 3523]
2016-10-20 21:38:24
【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)的更多相关文章
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 【POJ】2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
- POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈
题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...
- poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
- UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向 ...
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...
- poj2942 Knights of the Round Table[点双+二分图染色]
首先转化条件,把无仇恨的人连边,然后转化成了求有哪些点不在任何一个奇环中. 一个奇环肯定是一个点双,所以想到处理出所有点双,但是也可能有的点双是一个偶环,有的可能是偶环和奇环混杂,不好判. 考察奇环性 ...
- POJ 2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
- [POJ2942][LA3523]Knights of the Round Table
[POJ2942][LA3523]Knights of the Round Table 试题描述 Being a knight is a very attractive career: searchi ...
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
随机推荐
- Eclipse下使用Hadoop单机模式调试MapReduce程序
在单机模式下Hadoop不会使用HDFS,也不会开启任何Hadoop守护进程,所有程序将在一个JVM上运行并且最多只允许拥有一个reducer 在Eclipse中新创建一个hadoop-test的Ja ...
- Base62编码与62进制
Base62编码 Base62编码与Base64编码类似,都用于数据内容编码.基本原理请参看<Base64算法>. import java.io.ByteArrayOutputStream ...
- modelsim remote
远程桌面登陆我的台式机上的账号,然后运行modelsim 出现该问题: Unable to checkout a viewer license necessary for use of the Mod ...
- WebWork2和Spring MVC Framework的比较
http://daihaixiang.blog.163.com/blog/static/3830134200711411515336/ WebWork2和Spring MVC Framework的比较 ...
- Linux云主机安装JDK,配置hadoop的详细方式
云主机我使用的是青云的,还有好多其他品牌,比如阿里云 unitedstack 等等. 注册完青云后,会有试用券发到账户,可以利用此券试用其服务. 1 首先创建好一个主机,按照提示选择好系统,创建好一个 ...
- A Swift Tour(3) - Functions and Closures
Functions and Closures 使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如: func greet(name: ...
- Java分布式处理技术(RMI,JDNI)
http://hedaoyuan.blog.51cto.com/4639772/813702 1.1 RMI的基本概念 1.1.1 什么是RMI RMI(Remote Method Invocatio ...
- autorelease 的基本使用
5-autorelease 的基本使用 0,引入 Person *p = [Persom new];[p release]; [p run]; [p run]; // 希望不立即释放,待 run执行完 ...
- asp:DateDiff 函数
DateDiff 函数 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目. 语法 DateDiff(interval, date1, date2[, firstdayofwe ...
- Ladder免费试用版
Ladder这款vpn软件最近在play中国区排名非常高,节节高升,可见大家还真需要这东西,软件做的不错,大家可以在试用版中升级到正式版. 下载地址