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



Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 11661   Accepted: 3824

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

题目大意:有n个骑士,m个仇视关系,每一个关系a b表示a与b老死不相往来,。

如今亚瑟王想要定期举办圆桌会议。圆桌会议要求到的骑士围成一圈。也就是每一个骑士一定会有左右两个相邻的骑士。要求相邻的骑士间不可有直接的仇视关系。

举行圆桌会议的骑士数目必须大于1。而且必须为奇数。

问须要剔除多少骑士才干正常举办会议。

有一个意思我没读出来,就是并不须要留下的骑士能參与同一场会议,会议能够举行多场,仅仅要留下的骑士能參与当中一场就可以。

这样建立补图,也就是骑士友好关系的图后。处于不同的双连通子图中的点一定无法出席同一场圆桌会议。由于这些点间要么是仇恨关系,要么仅仅有一条友好关系,无法成环。

这样范围就缩小到同一双连通分量中。

这里用到了一个结论,对于一个双连通分量,假设存在奇环,那么这个双连通分量中的每个点都一定会存在于至少一个奇环中。

由于不论什么一个点都有两条以上到这个奇环的路径,两条路径在连接奇环上的两个点,能够把奇环变为偶链和奇链 这样依据该点到这两个点间点个数的奇偶性进行选择 就保证一定能够构成奇环。

相同,假设不存在奇环,则全部双连通分量中的点都不存在于不论什么一个奇环中。

代码例如以下:

  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <queue>
  8. #include <stack>
  9. #include <list>
  10. #include <algorithm>
  11. #include <map>
  12. #include <set>
  13. #define LL long long
  14. #define Pr pair<int,int>
  15. #define fread() freopen("in.in","r",stdin)
  16. #define fwrite() freopen("out.out","w",stdout)
  17.  
  18. using namespace std;
  19. const int INF = 0x3f3f3f3f;
  20. const int msz = 10000;
  21. const int mod = 1e9+7;
  22. const double eps = 1e-8;
  23.  
  24. bool can[2333];
  25. bool in[2333];
  26. bool mp[2333][2333];
  27. bool vis[2333];
  28. int col[2333];
  29. int dfn[2333],low[2333];
  30. stack <int> s;
  31. int n,tim;
  32.  
  33. bool cal(int u)
  34. {
  35. // printf("%d\n",u);
  36. queue <int> q;
  37. q.push(u);
  38. memset(col,-1,sizeof(col));
  39. col[u] = 1;
  40.  
  41. while(!q.empty())
  42. {
  43. u = q.front();
  44. q.pop();
  45. for(int i = 1; i <= n; ++i)
  46. {
  47. if(u != i && in[i] && !mp[u][i])
  48. {
  49. // printf("%d->%d %d %d\n",u,i,col[u],col[i]);
  50. if(col[i] == -1)
  51. {
  52. col[i] = col[u]^1;
  53. q.push(i);
  54. }else if(col[i] == col[u]) return true;
  55. }
  56. }
  57. }
  58. return false;
  59. }
  60.  
  61. void tarjan(int u,int pre)
  62. {
  63. s.push(u);
  64. dfn[u] = low[u] = tim++;
  65. vis[u] = 1;
  66. for(int i = 1; i <= n; ++i)
  67. {
  68. if(i == u || i == pre || mp[u][i]) continue;
  69. if(!vis[i])
  70. {
  71. tarjan(i,u);
  72. low[u] = min(low[u],low[i]);
  73. if(low[i] >= dfn[u])
  74. {
  75. memset(in,0,sizeof(in));
  76. while(s.top() != i)
  77. {
  78. in[s.top()] = 1;
  79. s.pop();
  80. }
  81. in[i] = 1;
  82. s.pop();
  83. in[u] = 1;
  84. if(cal(u))
  85. {
  86. for(int i = 1; i <= n; ++i)
  87. if(in[i]) can[i] = 1;
  88. }
  89. }
  90. }else low[u] = min(low[u],dfn[i]);
  91. }
  92. }
  93.  
  94. int main()
  95. {
  96. //fread();
  97. //fwrite();
  98.  
  99. int m,u,v;
  100. while(~scanf("%d%d",&n,&m) && (n+m))
  101. {
  102. memset(mp,0,sizeof(mp));
  103. while(m--)
  104. {
  105. scanf("%d%d",&u,&v);
  106. mp[u][v] = mp[v][u] = 1;
  107. }
  108.  
  109. memset(vis,0,sizeof(vis));
  110. memset(can,0,sizeof(can));
  111. tim = 0;
  112. for(int i = 1; i <= n; ++i)
  113. if(!vis[i]) tarjan(i,i);
  114.  
  115. int ans = 0;
  116. for(int i = 1; i <= n; ++i)
  117. ans += can[i];
  118.  
  119. printf("%d\n",n-ans);
  120. }
  121.  
  122. return 0;
  123. }



【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)的更多相关文章

  1. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  2. POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  3. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

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

  4. POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug

    题面还好,就不描述了 重点说题解: 由于仇恨关系不好处理,所以可以搞补图存不仇恨关系, 如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环 所以跑点双联通分量 求点双联通分量我用的是向栈中pus ...

  5. poj 2942 Knights of the Round Table - Tarjan

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  6. POJ 2942 Knights of the Round Table

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

  7. POJ 2942 Knights of the Round Table - from lanshui_Yang

    Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...

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

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

  9. POJ 2942.Knights of the Round Table (双连通)

    简要题解: 意在判断哪些点在一个图的  奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...

随机推荐

  1. BPM不同表单之间子表的赋值

    上次写的是同一个表单的子表之间赋值,这次是不同表单之间子表的赋值 首先,我们给需要赋值的表单添加一个复制按钮 $.MvcSheet.AddAction({            Action: &qu ...

  2. adb 通过 WiFi 连接 Android 设备

    PC 和 Android 设备连接在同一个局域网. 查看 Android 设备的 IP:设置 > WLAN > 选择连接的WiFi > 查看IP地址. PC 端执行: ping &l ...

  3. mysqldump+mydumper+xtrabackup备份原理流程

    mysqldump备份原理 备份的基本流程如下: 1.调用FTWRL(flush tables with read lock),全局禁止读写 2.开启快照读,获取此时的快照(仅对innodb表起作用) ...

  4. uva 1658 Admiral 【 最小费用最大流 】

    拆点,每个点拆成 i,i' 在i 和i‘之间连一条费用为0,容量为1的边,就可以保证每个点只经过一次 特殊的点,1和n之间,,,n和2*n之间连一条费用为0,容量为2的边,可以求出两条路径 #incl ...

  5. FastFDS常用命令

    1.启停fastdfs相关服务 #start fastdfs  启动服务 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart /usr/loca ...

  6. tsar源码分析

    从modules/mod_cpu.c入手 ./devel/tsar.h module 结构体: struct module { char name[LEN_32]; char opt_line[LEN ...

  7. JavaScript 三要素

    一个完整的JavaScript 实现由3部分组成: ECMACcript ECMAScript 规定了这门语言的下列组成部分: 语法 类型 语句 关键字.保留字 操作符 对象为什么要使用DOM?   ...

  8. HDU 4513 吉哥系列故事——完美队形II( Manacher变形 )

    链接:传送门 思路:根据完美队形的定义,可以得知,完美队形实质上是 回文串 + 序列出现峰,因为是在回文串中再次增加了一个要求,所以可以对 Manacher 进行改造,改造的部分应该为暴力匹配的循环 ...

  9. P1423 小玉在游泳

    ... 题目描述 小玉开心的在游泳,可是她很快难过的发现,自己的力气不够,游泳好累哦.已知小玉第一步能游2米,可是随着越来越累,力气越来越小,她接下来的每一步都只能游出上一步距离的98%.现在小玉想知 ...

  10. 一步步理解linux字符设备驱动框架(转)

    /* *本文版权归于凌阳教育.如转载请注明 *原作者和原文链接 http://blog.csdn.net/edudriver/article/details/18354313* *特此说明并保留对其追 ...