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, 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 黑白着色+点双连通分量


下一篇:南非的5DT数据手套使用说明