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.

  题目大意 有n个骑士和m个对憎恨关系,每次圆桌会议至少3人参加且人数必须为奇数,互相憎恨的骑士不能邻座。问有多少骑士不可能参加任何一场圆桌会议。

  首先直接按照憎恨关系建图总之我做不出来。所以考虑补图,补图的意义是可以邻座的关系建出来的图(正难则反)。那么一个骑士可以参加某场圆桌会议就等价于存在一个长度为奇数的简单环包含它。

  所以可以考虑把所有点双连通分量求出来,然后dfs染色进行判断。如果一个点-双联通分量中存在一个奇环,那么这个点-双连通分量内的所有奇环覆盖了这中间所有点。

  假设存在一个奇环,那么考虑任意一个不在这个奇环内的点$p$,以及任意一个奇环上的点$q$。由点双性质有,存在两条公共点只有$p, q$的路径使得从$p$到达$q$。设两条路径到达环的第一个点分别为$u, v$。显然存在一种方案使得$u\neq v$,否则可以得到$u$是一个割点。$u, v$间在环上有两条路径$l_1, l_2$,显然$2\nmid |l_1| - |l_2|$。考虑$(p \rightarrow u) - l_1 - (v \rightarrow p)$和$(p \rightarrow u) - l_2 - (v \rightarrow p)$这两个环,它们环长的奇偶性显然不同。所以存在一个奇环包含$p$。

  所以如果一个点双连通分量不能够被染色,就可以把它包含的所有点标为可以参加某场会议。

Code

 /**
* poj
* Problem#2942
* Accepted
* Time: 1141ms
* Memory: 1232k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
typedef bool boolean;
#define smin(a, b) a = min(a, b) ///map template starts
typedef class Edge{
public:
int end;
int next;
int w; Edge(const int end = , const int next = -, const int w = ):end(end), next(next), w(w) { }
}Edge; typedef class MapManager{
public:
int ce;
int *h;
vector<Edge> edge; MapManager() { }
MapManager(int points):ce() {
h = new int[(const int)(points + )];
memset(h, -, sizeof(int) * (points + ));
} inline void addEdge(int from, int end, int w) {
edge.push_back(Edge(end, h[from], w));
h[from] = ce++;
} inline void addDoubleEdge(int from, int end, int w) {
addEdge(from, end, w);
addEdge(end, from, w);
} inline void clear() {
delete[] h;
edge.clear();
} Edge& operator [] (int pos) {
return edge[pos];
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
#define m_endpos -1
///map template ends int n, m;
boolean hated[][];
MapManager g; inline boolean init() {
scanf("%d%d", &n, &m);
if(!n && !m) return false;
memset(hated[], false, sizeof(hated[]) * n);
g = MapManager(n);
for(int i = , u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
hated[u][v] = hated[v][u] = true;
}
return true;
} int cnt = ;
int cc = ;
stack<int> s;
int visitID[];
int exitID[];
boolean visited[];
int counter[];
vector< vector<int> > contains;
inline void init_tarjan() {
for(int i = ; i <= n; i++)
for(int j = i + ; j <= n; j++)
if(i != j && !hated[i][j])
g.addDoubleEdge(i, j, ); cnt = , cc = ;
memset(visited, false, sizeof(boolean) * (n + ));
} void tarjan(int node, int fae) {
visitID[node] = exitID[node] = ++cnt;
visited[node] = true; for(int i = m_begin(g, node); i != m_endpos; i = g[i].next) {
if(i == fae) continue;
int& e = g[i].end;
if(!visited[e]) {
s.push(i);
tarjan(e, i ^ );
smin(exitID[node], exitID[e]);
if(exitID[e] >= visitID[node]) {
int j = ;
cc++;
vector<int> l;
do {
j = s.top();
s.pop();
g[j].w = g[j ^ ].w = cc;
l.push_back(j);
l.push_back(j ^ );
// printf("%d %d %d\n", g[j ^ 1].end, g[j].end, cc);
} while (j != i);
contains.push_back(l);
}
} else {
smin(exitID[node], visitID[e]);
if(visitID[e] < visitID[node])
s.push(i);
}
}
} int color[];
boolean dfs(int node, int limit, int c) {
if(color[node] != -) return color[node] == c;
color[node] = c;
for(int i = m_begin(g, node); i != m_endpos; i = g[i].next) {
if(g[i].w != limit) continue;
int& e = g[i].end;
if(!dfs(e, limit, c ^ )) return false;
}
return true;
} int res;
boolean aced[];
inline void solve() {
res = ;
memset(aced, false, sizeof(int) * (n + ));
// cout << cc << endl;
for(int c = ; c < (signed)contains.size(); c++) {
memset(color, -, sizeof(int) * (n + ));
boolean aFlag = dfs(g[contains[c][]].end, c + , );
if(!aFlag)
for(int i = ; i < (signed)contains[c].size(); i++)
aced[g[contains[c][i]].end] = true;
}
for(int i = ; i <= n; i++)
if(!aced[i])
res++;
printf("%d\n", res);
} inline void clear() {
g.clear();
contains.clear();
} int main() {
while(init()) {
init_tarjan();
for(int i = ; i <= n; i++)
if(!visited[i])
tarjan(i, -);
solve();
clear();
}
return ;
}

poj 2942 Knights of the Round Table - Tarjan的更多相关文章

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

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

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

  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 补图+tarjan求点双联通分量+二分图染色+debug

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

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

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

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

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

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

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

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

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

随机推荐

  1. Ecshop 表结构 字段说明

    ecs_account_log 用户帐号情况记录表,包括资金和积分等 log_id mediumint 自增ID号user_id mediumint 用户登录后保存在session中的id号,跟use ...

  2. Nginx配置文件具体配置解释

    Nginx配置文件具体配置解释   #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错 ...

  3. Bootstrap-媒体查询-屏幕大小

    .container{padding:0 15px; margin:0 auto;} .container:before{ content: ''; display: table;/*防止第一个子元素 ...

  4. EL语言表达式 (一)【语法和特点】

    一.基本语法规则: EL表达式语言以“${”开头,以"}"结尾的程序段,具体格式如下: ${expression} 其中expression:表示要指定输出的内容和字符串以及EL运 ...

  5. C# 如何批量修改集合元素的属性值?

    我们往往会遇到要批量修改集合中元素的值,最笨的办法就是foreach循环,但本文介绍几种优雅的方法. 首先,我们准备好元素类和初始集合: 下面就是几种方法,目前并没有对性能做进一步的测试,有兴趣的童鞋 ...

  6. C++的类型转换

    一.类型转换名称和语法 1.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a    2.C++风格的类型转换提供了4种类型转换操作符来应对 ...

  7. HDU1530 最大流问题

    第一次写Dinic 然后贴一下 最基础的网络流问题 嘎嘎: #include <iostream> #include<cstdio> #include<string.h& ...

  8. PGPDesktop在win7环境下的安装和使用

    PGPDesktop在win7环境下的安装和使用 PGP的简介 PGP(Pretty Good Privacy),是一个基于RSA公钥加密体系的邮件加密软件,它提供了非对称加密和数字签名,是目前非常流 ...

  9. 利用可排序Key-Value DB构建时间序列数据库(简论)

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5855064. ...

  10. tensorflow学习2-线性拟合和神经网路拟合

    线性拟合的思路: 线性拟合代码: import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #%%图形绘制 ...