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. Linux Bash终端快捷键小结

    Ctrl + A  定位至行首 Ctrl + E  定位至行尾 Ctrl + U  向前删除至行首 Ctrl + K  向后删除至行尾 Ctrl + L  清屏

  2. Adapter 适配器模式

    将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 目标接口(Target):客户所期待的接口.目标可以是具体的或抽象的类,也可 ...

  3. loadrunner简单的例子(demo)

    刚刚做了一个loadrunner进行负载测试,把步骤截图给大伙看看.一共三个步骤 一创建/编辑脚本 二运行负载测试 三分析测试结果 首先是第一步的流程:第一步创建/编辑脚本 图一 图二 图三 图四 图 ...

  4. Click ListView Item跳转Activity

    今天学习了ListView点击Item跳转,修改上一篇代码bindData方法 lv.setOnItemClickListener(new OnItemClickListener() { public ...

  5. 使用普通Windows服务创建Quartz.Net服务项目

    Quartz.NET 项目地址 http://quartznet.sourceforge.net/ 源码下载地址:Quartz.Net.2.0 首先创建Quartz.Net.2.0解决方案,添加 Wi ...

  6. EasyUI中combotree允许多选的时候onSelect事件会重复触发onCheck事件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAADkCAIAAACOkmAuAAAgAElEQVR4nO2dW2wc15nnO0EQBJsdzA

  7. .Net Framework 开发Http协议

    一.Http的基本原理 1.HTTP协议的运作方式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HTTP协议是基于请求/响应范式的.一个客户机与服务器建立连接后,发送一个请求 ...

  8. js【输入一个日期】返回【当前12个月每月最后一天】

    Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...

  9. 对C++ Primer的10.3.9单词转换的思考

    这篇代码有几个知识点可以复习一下,而且小白学到了新知识o(╯□╰)o #include <iostream> #include <string> #include <ma ...

  10. WPF页面跳转

    WPF页面跳转有两种:一种是windows,另外一种是page  1:windows页面跳转windows 页面跳转相信学过winform编程的哥们都知道,先实例化该窗体然后show一下就可以了.eg ...