Knights of the Round Table
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 5892 | Accepted: 1832 |
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:
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.)
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 .
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.
Source
Central Europe 2005
======================================================================================================
构造补图,求双连通分量(点),对于每一个双连通分量,判断是否能二分染色,若不能则标记对应节点,最后统计一遍~
感觉做的不是很顺畅,还是要多思考~
===================================================================================================================
Code:
======================================================================================================
题意:
亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求:
1、 相互憎恨的两个骑士不能坐在直接相邻的2个位置;
2、 出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果。
如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和平会强制把他剔除出骑士团。
现在给定准备去开会的骑士数n,再给出m对憎恨对(表示某2个骑士之间使互相憎恨的),问亚瑟王至少要剔除多少个骑士才能顺利召开会议?
注意:1、所给出的憎恨关系一定是双向的,不存在单向憎恨关系。
2、由于是圆桌会议,则每个出席的骑士身边必定刚好有2个骑士。即每个骑士的座位两边都必定各有一个骑士。
3、一个骑士无法开会,就是说至少有3个骑士才可能开会。
相关定理:
(1) 如果一个双连通分量内的某些顶点在一个奇圈中(即双连通分量含有奇圈),那么这个双连通分量的其他顶点也在某个奇圈中;
(2) 如果一个双连通分量含有奇圈,则他必定不是一个二分图。反过来也成立,这是一个充要条件。
做法:构造补图,求双连通分量(点),对于每一个双连通分量,判断是否能二分染色,若不能则标记对应节点,最后统计一遍~
感觉做的不是很顺畅,还是要多思考~
===================================================================================================================
Code:
program poj2942;
const
MaxN=1004;
type
link=^node;
node=record
x:longint;
vis:boolean;
back,next:link;
end;
var
map:array[1..MaxN,1..MaxN] of boolean;
g:array[1..MaxN] of link;
low,dfn,col:array[1..MaxN] of longint;
part,ok:array[1..MaxN] of boolean;
stack:array[1..MaxN*MaxN] of link;
index,top,n,m:longint;
procedure Conn(i,j:longint);
var p:link;
begin
new(p);
p^.x:=j;
p^.vis:=false;
p^.next:=g[i];
g[i]:=p;
new(p);
p^.x:=i;
p^.vis:=false;
p^.next:=g[j];
g[j]:=p;
g[i]^.back:=g[j];
g[j]^.back:=g[i];
end;
function Find(u:longint):boolean;
var p:link;
begin
p:=g[u];
while p<>nil do begin
if part[p^.x] then begin
if (col[p^.x]=0) then begin
col[p^.x]:=3-col[u];
exit(Find(p^.x));
end else if col[p^.x]=col[u] then exit(true);
end;
p:=p^.next;
end;
exit(false);
end;
procedure Calc(u:longint);
var p:link; i:longint;
begin
fillchar(part,sizeof(part),false);
repeat
p:=stack[top];
dec(top);
part[p^.x]:=true;
part[p^.back^.x]:=true;
until (p^.back^.x=u)or(top=0);
fillchar(col,sizeof(col),0);
col[u]:=1;
if Find(u) then
for i:=1 to n do if part[i] then ok[i]:=true;
end;
function Min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure Tarjan(u:longint);
var v:longint; p:link;
begin
inc(index);
dfn[u]:=index;
low[u]:=index;
p:=g[u];
while p<>nil do begin
if not p^.vis then begin
v:=p^.x;
p^.vis:=true;
p^.back^.vis:=true;
inc(top); stack[top]:=p;
if dfn[v]=0 then begin
Tarjan(v);
low[u]:=Min(low[v],low[u]);
if low[v]>=dfn[u] then Calc(u);
end else low[u]:=Min(low[u],dfn[v]);
end;
p:=p^.next;
end;
end;
procedure Scan;
var i,j,a,b:longint;
begin
fillchar(map,sizeof(map),true);
for i:=1 to m do begin
readln(a,b);
map[a,b]:=false;
map[b,a]:=false;
end;
for i:=1 to MaxN do g[i]:=nil;
for i:=1 to n do
for j:=i+1 to n do
if map[i,j] then Conn(i,j);
end;
procedure Solve;
var i,cnt:longint;
begin
fillchar(ok,sizeof(ok),false);
fillchar(dfn,sizeof(dfn),0);
fillchar(low,sizeof(low),0);
index:=0; top:=0;
for i:=1 to n do
if dfn[i]=0 then Tarjan(i);
cnt:=0;
for i:=1 to n do
if ok[i] then inc(cnt);
writeln(n-cnt);
end;
procedure Main;
begin
while not eof do begin
readln(n,m);
if (n=0)and(m=0)then break;
Scan;
Solve;
end;
end;
begin
assign(input,'poj2942.in'); reset(input);
assign(output,'poj2942.out'); rewrite(output);
Main;
close(input); close(output);
end.