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 (点双连通分量+染色问题?)的更多相关文章

  1. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  2. 【POJ】2942 Knights of the Round Table(双连通分量)

    http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...

  3. POJ2942 Knights of the Round Table 点双连通分量,逆图,奇圈

    题目链接: poj2942 题意: 有n个人,能够开多场圆桌会议 这n个人中,有m对人有仇视的关系,相互仇视的两人坐在相邻的位置 且每场圆桌会议的人数仅仅能为奇书 问有多少人不能參加 解题思路: 首先 ...

  4. poj 2942 Knights of the Round Table(点双连通分量+二分图判定)

    题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...

  5. UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)

    题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向 ...

  6. POJ2942 Knights of the Round Table 点双连通分量 二分图判定

    题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...

  7. poj2942 Knights of the Round Table[点双+二分图染色]

    首先转化条件,把无仇恨的人连边,然后转化成了求有哪些点不在任何一个奇环中. 一个奇环肯定是一个点双,所以想到处理出所有点双,但是也可能有的点双是一个偶环,有的可能是偶环和奇环混杂,不好判. 考察奇环性 ...

  8. POJ 2942 Knights of the Round Table(双连通分量)

    http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...

  9. [POJ2942][LA3523]Knights of the Round Table

    [POJ2942][LA3523]Knights of the Round Table 试题描述 Being a knight is a very attractive career: searchi ...

  10. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

随机推荐

  1. ACM——3n+1

    3n+1 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:937            测试通过:386 描述 对于任意大于1的自然 ...

  2. Hyper-V Windows 8.1 & Windows Server 2012 R2 Q&A

    从Windows8开始,x64位系统自带Hyper-V功能,很多开发者和专业用户往往希望利用的Microsoft提供的这一免费功能,但是微软在这方面并不是最佳. 主要写几个大家经常遇到的问题. Win ...

  3. Runtime运行时学习(一)

    其实Runtime已经开源: 下载objc4-437.1.tar.gz来看看源码: 参考: http://blog.cocoabit.com/2014-10-06-yi-li-jie-objctive ...

  4. iOS 获取当前媒体音量

    #import <AVFoundation/AVAudioSession.h> AVAudioSession *audioSession = [AVAudioSession sharedI ...

  5. 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结

    请求Header原帖地址:http://technique-digest.iteye.com/blog/1174581 响应Header原帖地址:http://blog.pfan.cn/hurongl ...

  6. SomeThing of Memcache

    Memcache for .net 文章一: http://blog.csdn.net/dinglang_2009/article/details/6917794 不定时更新

  7. PHP pear安装出现 Warning: require_once(Structures/Graph.php)...错误

    今天在WINDOWS安装pear,一路无阻很顺利安装完成,接着想安装下pear email包来玩下,但接下来却报: Warning: require_once(Structures/Graph.php ...

  8. HDU 3008 Warcraft(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3008 题目大意:人有100血和100魔法,每秒增加 t 魔法(不能超过100).n个技能,每个技能消耗 ...

  9. (转)IOS内存管理 retain release

    obj-c本质就是"改进过的c语言",大家都知道c语言是没有垃圾回收(GC)机制的(注:虽然obj-c2.0后来增加了GC功能,但是在iphone上不能用,因此对于iOS平台的程序 ...

  10. android 开源框架推荐

    同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JS ...