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
   题目大意:有n个骑士,他们之间可能有憎恨关系,例如:骑士a 憎恨 骑士b ,那么骑士b 也一定 憎恨 骑士a。现在要给这n个骑士开会,会议需要满足以下要求:
   1、参加会议的骑士数量必须是奇数。
   2、会议选用圆形桌子,即骑士们开会时围成一个圈。
   3、要求开会时任意一个骑士与相邻的两个骑士之间没有憎恨关系。
当然,会议可以开很多场,而且一个骑士也可以参加很多场会议(如果能参加的话),问:一场会议也不能参加的骑士数量是多少?
   解题思路:
   1、先说一下建图方法:将两个没有憎恨关系的骑士之间连接一条边(无向边),代表开会时这两个骑士可以相邻。
   2、判断一个骑士能否参加会议,就是判断在这个无向图中有没有存在一个简单的奇圈包含这个骑士(简单奇圈是指由奇数个顶点组成的圈,并且这些顶点互不相同)。
   3、在同一个简单奇圈上的点必然在同一个双连通分量中,所以要找出图中所有的双连通分量,然后判断这个双连通分量中有没有奇圈。二分图中是没有奇圈的,如果一个双联通分量不是二分图,那么一定存在奇圈(此处不再证明),所以只需判断一个双连通分量是不是二分图即可,如果是二分图,那么这个连通分量中每个点都可以参加会议。
请看代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
inline void RD(int &a)
{
a = 0 ;
char t ;
do
{
t = getchar() ;
}
while (t < '0' || t > '9') ;
a = t - '0' ;
while ((t = getchar()) >= '0' && t <= '9')
{
a = a * 10 + t - '0' ;
}
}
inline void OT(int a)
{
if(a >= 10)
{
OT(a / 10) ;
}
putchar(a % 10 + '0') ;
}
const int MAXN = 1005 ;
typedef struct edge
{
int u ;
int v ;
} E ;
vector<int>vert[MAXN] ;
vector<int>bscnt[MAXN] ; // 记录每个双连通分量里的顶点
int blcnt[MAXN] ; // 记录每个顶点属于哪个连通分量
bool ha[MAXN][MAXN] ; // 标记两个骑士是否相互憎恨
bool vis[MAXN] ;
int n , m ;
int dfn[MAXN] ;
int low[MAXN] ;
int tmpdfn ;
int top ;
E stap[MAXN * MAXN] ;
bool odd[MAXN] ;
short color[MAXN] ;
int scnt ;
void clr()
{
mem(ha , 0) ;
mem(dfn , 0) ;
mem(low , 0) ;
mem(vis , 0) ;
mem(blcnt , 0) ;
mem(color , 0) ;
mem(odd , 0) ;
int i ;
for(i = 0 ; i <= n ; i ++)
{
vert[i].clear() ;
bscnt[i].clear() ;
}
top = -1 ;
tmpdfn = 0 ;
scnt = 0 ;
}
void tarjan(int u , int fa)
{
int son = 0 ;
vis[u] = true ;
dfn[u] = low[u] = ++ tmpdfn ;
int i ;
for(i = 0 ; i < vert[u].size() ; i ++)
{
int v = vert[u][i] ;
E e ;
e.u = u ;
e.v = v ;
if(!vis[v])
{
stap[++ top] = e ;
son ++ ;
tarjan(v , u) ;
low[u] = min(low[u] , low[v]) ;
if(low[v] >= dfn[u])
{
scnt ++ ;
int tu , tv ;
while (1)
{
E tmp = stap[top --] ;
tu = tmp.u ;
tv = tmp.v ;
if(blcnt[tu] != scnt) // 注意此处的条件,由于关节点属于
//不同的连通分量,所以条件不能写成(!blcnt[tu])
{
blcnt[tu] = scnt ;
bscnt[scnt].push_back(tu) ;
}
if(blcnt[tv] != scnt)
{
blcnt[tv] = scnt ;
bscnt[scnt].push_back(tv) ;
}
if(tu == u && tv == v)
break ;
}
}
}
else if(v != fa && dfn[v] < dfn[u]) // 注意此处的判断条件,不要漏掉 dfn[v] < dfn[u]
{
stap[++ top] = e ;
low[u] = min(low[u] , dfn[v]) ;
} }
}
void init()
{
clr() ;
int i , j ;
for(i = 0 ; i < m ; i ++)
{
int a , b ;
RD(a) ;
RD(b) ;
ha[a][b] = ha[b][a] = true ;
}
for(i = 1 ; i <= n ; i ++) // 建图
{
for(j = 1 ; j <= n ; j ++)
{
if(!ha[i][j] && i != j)
vert[i].push_back(j) ;
}
}
}
bool isbg(int u , int c) //判断是否为二分图
{
int i ;
for(i = 0 ; i < vert[u].size() ; i ++)
{
int v = vert[u][i] ;
if(blcnt[v] != c)
continue ;
if(color[v] == color[u])
return false ;
if(!color[v])
{
color[v] = 3 - color[u] ;
if(!isbg(v , c))
return false ;
}
}
return true ;
}
void solve()
{
mem(vis , 0) ;
int i ;
for(i = 1 ; i <= n ; i ++)
{
if(!vis[i])
{
tarjan(i , -1) ;
}
}
for(i = 1 ; i <= scnt ; i ++)
{
int j ;
int tmp ;
mem(color , 0) ;
for(j = 0 ; j < bscnt[i].size() ; j ++)
{
tmp = bscnt[i][j] ;
blcnt[tmp] = i ;
}
color[tmp] = 1 ;
if(!isbg(tmp , i))
{
for(j = 0 ; j < bscnt[i].size() ; j ++)
{
tmp = bscnt[i][j] ;
odd[tmp] = true ;
}
}
}
int ans = n ;
for(i = 1 ; i <= n ; i ++)
{
if(odd[i])
ans -- ;
}
OT(ans) ;
puts("") ;
}
int main()
{
while (scanf("%d%d" , &n , &m) != EOF)
{
if(n == 0 && m == 0)
break ;
init() ;
solve() ;
}
return 0 ;
}
												

POJ 2942 Knights of the Round Table - from lanshui_Yang的更多相关文章

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

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

  2. 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 ...

  3. POJ 2942 Knights of the Round Table

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

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

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

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

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

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

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

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

    题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...

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

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

  9. POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)

    题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...

随机推荐

  1. 【慎思堂】之JS牛腩总结

    一 JS基础 1-定义 Javascript是一种脚本语言/描述语言,是一种解释性语言.用于开发交互式web网页,使得网页和用户之间实现了一种实时性的.动态的.交互性的关系,使网页包含更多活跃的元素和 ...

  2. boost::asio网络传输错误码的一些实验结果(recv error_code)

    错误码很重要,可以由此判断网络连接到底发生了神马事情,从而驱动高层逻辑的行为.只有笼统的错误码判断的网络层是不够规范的,鄙人觉得有些错误码还是需要在网络层就区分开的,特此记录一些当前实验的错误码以及发 ...

  3. asp.net web api帮助文档的说明

    为asp.net的mvc web api填写自己的帮助文档 1. 加入Help的area(能够通过命令行或其它方式加入) 命令行:Install-Package Microsoft.AspNet.We ...

  4. 移植一个开源点餐网到SAE平台上

    记得以前我准备弄个点餐网的,但是由于一些原因没有做下去. 前几天将网上的一个点餐源码移植到了SAE上,网址http://diancan4sae.sinaapp.com. 我想做个外卖网,先选一个学校周 ...

  5. UVA 116 Unidirectional TSP(dp + 数塔问题)

     Unidirectional TSP  Background Problems that require minimum paths through some domain appear in ma ...

  6. 辛星与您解读PHP页面跳转的几种实现方式

    因为页面跳转的使用是很频繁的,因此这里给出几种方式,事实上我想我并没有归纳全,毕竟函数那么多,要一下想起来还是特别麻烦的,于是,想到哪里就记到哪里把,等着以后再整理汇总. 第一种方式就是使用heade ...

  7. 如何关闭android studio开发环境自动保存

    使用DW习惯了现在转到学习开发android,请问怎样关闭android studio的自动保存功能,然后按ctrl+s进行保存,因为有时候代码不想让其保存,他也自动保存了. File -> S ...

  8. 14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件

    14.5.4 InnoDB File-Per-Table Tablespaces 每个表一个文件 从历史上看, 所有的InnoDB 表和索引是存储在system 表空间, 这个整体的方法是针对机器专注 ...

  9. C++著名类库和C++标准库介绍

    C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...

  10. codeforces 325B Stadium and Games

    这道题思路很简单,设刚开始队伍数为d=2^p*x,其中x是奇数,则比赛场次n=(2^p-1)*x+(x-1)*x/2,然后从0开始枚举p的值,接着解一元二次方程x^2+(2^(p+1)-3)x-2*n ...