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

Description

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

Hint

Huge input file, 'scanf' recommended to avoid TLE. 

Source


白书
题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1。2.总人数为奇数。3.有仇恨的骑士不能挨着坐。问有几个骑士不能和任何人形成任何的圆圈。

求点双连通分量,然后每个分量二分图染色,如果不是二分图,则一定可以构造出奇环经过每个点
点双连通分量就是求割点,割点分开的每一块就是一个bcc,用一个栈存边(没有公共边,割点是公共点,每个bcc只有一个割点)
 
注意割点会被ok[]=1多次,所以不能在过程中纪录ans
还有染色后记得把col[割点]=0,以后还会用
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define C(x) memset(x,0,sizeof(x))
using namespace std;
const int N=,M=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n=,m,u,v,g[N][N];
struct edge{
int v,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
void buildGraph(){
memset(h,,sizeof(h)); cnt=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++) if(!g[i][j]) ins(i,j);
}
struct data{
int u,v;
data(int a=,int b=):u(a),v(b){}
}st[M<<];
int top=;
int dfn[N],low[N],iscut[N],belong[N],dfc=,bcc=;
int col[N],ok[N];
bool color(int u,int id){//printf("%d %d %d\n",u,col[u],id);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(belong[v]!=id) continue;
if(col[v]==col[u]) return ;//first do this
if(!col[v]){
col[v]=-col[u];
if(!color(v,id)) return ;
}
}
return ;
}
void dfs(int u,int fa){
dfn[u]=low[u]=++dfc;
int child=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!dfn[v]){
st[++top]=data(u,v);
child++;
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]){
iscut[u]=;
bcc++;
while(true){
int tu=st[top].u,tv=st[top--].v;
if(belong[tu]!=bcc) belong[tu]=bcc;
if(belong[tv]!=bcc) belong[tv]=bcc;
if(tu==u&&tv==v) break;
}
col[u]=;
if(!color(u,bcc))
for(int i=;i<=n;i++) if(belong[i]==bcc) ok[i]=;
col[u]=;//for cut vertex
}
}else if(dfn[v]<dfn[u]&&v!=fa){
st[++top]=data(u,v);//notice!!!
low[u]=min(low[u],dfn[v]);
}
}
if(child==&&fa==) iscut[u]=;
}
void BCC(){
dfc=bcc=;top=;
C(dfn);C(low);C(iscut);C(belong);
C(col);C(ok);
for(int i=;i<=n;i++) if(!dfn[i]) dfs(i,);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
memset(g,,sizeof(g));
for(int i=;i<=m;i++){u=read();v=read();g[u][v]=g[v][u]=;}
buildGraph();
BCC();
int ans=;
for(int i=;i<=n;i++) if(!ok[i]) ans++;
printf("%d\n",ans);
}
}
 

POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Knights of the Round Table-POJ2942(双连通分量+交叉染色)

    Knights of the Round Table Description Being a knight is a very attractive career: searching for the ...

  8. [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)

    建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点                             ...

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

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

随机推荐

  1. 微信JSApi支付~坑和如何填坑

    返回目录 微信一直用着不爽,这几天研究它的jsapi支付,即在微信内打开H5页面,完成支付的过程,在这个过程中,你将会遇到各种各样的问题,而大叔将把这些问题的解决方法写一下,希望可以给你带来帮助! 一 ...

  2. WinFrom窗体始终置顶

    调用WindowsAPI使窗体始终保持置顶效果,不被其他窗体遮盖: [DllImport("user32.dll", CharSet = CharSet.Auto)] privat ...

  3. iOS--使用UIImageView进行GIF动图播放

    大家好,好久没有跟新了.其实也就昨天到今天的时间. 前言:实际上,GIF动图文件中包含了一组图片及其信息数组,这些信息数据记录着这一组图片中各张图片的播放时长等信息,我们可以将图片和这些信息或取出来, ...

  4. Eclipse导入项目出现红色叹号的解决方法

    情景: 我在另一台电脑开发Java Web项目,开发环境为 JDK 1.7,Tomcat v7.0,然后导入另一台电脑上,开发环境为为 JDK 1.8,Tomcat v8.0. 问题: 导入项目出现红 ...

  5. junit4 assert类中的assert方法总结

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类. 1.assertTrue/False([String message,]boolean cond ...

  6. 解决WIN7与虚拟机CentOS的文件夹共享问题

    一.系统与软件 WIN7 64bit.VirtualBox 5.0.14.CentOS 6.5.SecureCRT 7.2.3 二.使用文件夹共享需要安装增强功能,但是安装时无法读取光盘iso文件 三 ...

  7. php实现设计模式之 简单工厂模式

    作为对象的创建模式,用工厂方法代替new操作. 简单工厂模式是属于创建型模式,又叫做静态工厂方法模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例. 工厂 ...

  8. jQuery拉开关闭帷幕

    查看效果:http://keleyi.com/keleyi/phtml/jqtexiao/7.htm HTML文件代码: <!DOCTYPE html PUBLIC "-//W3C// ...

  9. kmdjs指令大全

    调试 通过下面方式,可以输出kmdjs声称的类: <script src="../dist/kmd.js?debug" data-main="js/main&quo ...

  10. iOS之两个ImageView实现图片滚动

    原创作者:codingZero 导语 在不少项目中,都会有图片轮播这个功能,现在网上关于图片轮播的框架层出不穷,千奇百怪,笔者根据自己的思路,用两个imageView也实现了图片轮播,这里说说笔者的主 ...