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

  1. 5 5
  2. 1 4
  3. 1 5
  4. 2 5
  5. 3 4
  6. 4 5
  7. 0 0

Sample Output

  1. 2

Hint

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

Source


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

求点双连通分量,然后每个分量二分图染色,如果不是二分图,则一定可以构造出奇环经过每个点
点双连通分量就是求割点,割点分开的每一块就是一个bcc,用一个栈存边(没有公共边,割点是公共点,每个bcc只有一个割点)
 
注意割点会被ok[]=1多次,所以不能在过程中纪录ans
还有染色后记得把col[割点]=0,以后还会用
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define C(x) memset(x,0,sizeof(x))
  6. using namespace std;
  7. const int N=,M=1e6+;
  8. inline int read(){
  9. char c=getchar();int x=,f=;
  10. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  11. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  12. return x*f;
  13. }
  14. int n=,m,u,v,g[N][N];
  15. struct edge{
  16. int v,ne;
  17. }e[M<<];
  18. int h[N],cnt=;
  19. inline void ins(int u,int v){
  20. cnt++;
  21. e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
  22. cnt++;
  23. e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
  24. }
  25. void buildGraph(){
  26. memset(h,,sizeof(h)); cnt=;
  27. for(int i=;i<=n;i++)
  28. for(int j=i+;j<=n;j++) if(!g[i][j]) ins(i,j);
  29. }
  30. struct data{
  31. int u,v;
  32. data(int a=,int b=):u(a),v(b){}
  33. }st[M<<];
  34. int top=;
  35. int dfn[N],low[N],iscut[N],belong[N],dfc=,bcc=;
  36. int col[N],ok[N];
  37. bool color(int u,int id){//printf("%d %d %d\n",u,col[u],id);
  38. for(int i=h[u];i;i=e[i].ne){
  39. int v=e[i].v;
  40. if(belong[v]!=id) continue;
  41. if(col[v]==col[u]) return ;//first do this
  42. if(!col[v]){
  43. col[v]=-col[u];
  44. if(!color(v,id)) return ;
  45. }
  46. }
  47. return ;
  48. }
  49. void dfs(int u,int fa){
  50. dfn[u]=low[u]=++dfc;
  51. int child=;
  52. for(int i=h[u];i;i=e[i].ne){
  53. int v=e[i].v;
  54. if(!dfn[v]){
  55. st[++top]=data(u,v);
  56. child++;
  57. dfs(v,u);
  58. low[u]=min(low[u],low[v]);
  59. if(low[v]>=dfn[u]){
  60. iscut[u]=;
  61. bcc++;
  62. while(true){
  63. int tu=st[top].u,tv=st[top--].v;
  64. if(belong[tu]!=bcc) belong[tu]=bcc;
  65. if(belong[tv]!=bcc) belong[tv]=bcc;
  66. if(tu==u&&tv==v) break;
  67. }
  68. col[u]=;
  69. if(!color(u,bcc))
  70. for(int i=;i<=n;i++) if(belong[i]==bcc) ok[i]=;
  71. col[u]=;//for cut vertex
  72. }
  73. }else if(dfn[v]<dfn[u]&&v!=fa){
  74. st[++top]=data(u,v);//notice!!!
  75. low[u]=min(low[u],dfn[v]);
  76. }
  77. }
  78. if(child==&&fa==) iscut[u]=;
  79. }
  80. void BCC(){
  81. dfc=bcc=;top=;
  82. C(dfn);C(low);C(iscut);C(belong);
  83. C(col);C(ok);
  84. for(int i=;i<=n;i++) if(!dfn[i]) dfs(i,);
  85. }
  86. int main(){
  87. while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
  88. memset(g,,sizeof(g));
  89. for(int i=;i<=m;i++){u=read();v=read();g[u][v]=g[v][u]=;}
  90. buildGraph();
  91. BCC();
  92. int ans=;
  93. for(int i=;i<=n;i++) if(!ok[i]) ans++;
  94. printf("%d\n",ans);
  95. }
  96. }
 

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. jquery 之for 循环

    jquery 的 for 循环: 1. var userList = [11,22,33,44]; $.each(userList,function(i,item){ console.log(i, i ...

  2. img在div中居中的问题

    Img是内联元素,要设置其margin属性使其居中,就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中:(有的设计师为图片再加个div标签,然后通过 ...

  3. eclipse控制台下实现jdbc简单的增删改查测试

    1.现在MySQL中创建一个表 2.首先创建一个类 //导入的包 import java.sql.Connection;import java.sql.DriverManager;import jav ...

  4. java中抽象、分装、继承和多态的理解

    1.抽象.封装装.继承和多态是java面向对象编程的几大特点. 抽象:所谓抽象就是对某件事务,我们忽略我们不关心不需要的部分,提取我们想要的属性和行为,并且以代码的形式提现出来:例如我们需要对一个学生 ...

  5. Javascript:JSON总结

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于ECMAScript的一个子集,采用完全独立于语言的文本格式.这些特性使JSON成为理想的数据交换 ...

  6. jquery点击复选框触发事件给input赋值

    体验效果:http://keleyi.com/keleyi/phtml/jqtexiao/31.htm 代码如下: <!DOCTYPE html> <html xmlns=" ...

  7. 闲话js作用域

    js词法环境包括环境变量绑定及外部引用'函数创建的时候有个内部属性[[scope]],它指向当前函数的词法环境对象.而词法环境的外部引用一个词法环境'直到全局词法环境'它外部引用为null'这样就构成 ...

  8. javascript的函数(二)

    1. 函数的作用域 作用域是指变量的存在的范围.javascript中有两种作用域,一种是全局作用域,变量在整个程序中一直存在,另一种是函数作用域,变量只存在于函数体内部.在函数体外部声明的变量就是全 ...

  9. 获取WIFI密码

    在十年前,我还在上初中,班上只有极少数的富二代用得起手机:几年后诺基亚.摩托罗拉.三星手机开始盛行:近些年,安卓.苹果系统手机占据了基本整个市场,WIFI出变得越来越重要. Wifi万能钥匙数据库存储 ...

  10. wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)

    首先说几个最常用的关键字,"eq" 和 "=="等同,可以使用 "and" 表示并且,"or"表示或者."!& ...