Description
Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. The bride and groom sit at one end, opposite each other, and the bride wears an elaborate headdress that keeps her from seeing people on the same side as her. It is considered bad luck to have a husband and wife seated on the same side of the table. Additionally, there are several pairs of people conducting adulterous relationships (both different-sex and same-sex relationships are possible), and it is bad luck for the bride to see both members of such a pair. Your job is to arrange people at the table so as to avoid any bad luck.
Input
The input consists of a number of test cases, followed by a line containing 0 0. Each test case gives n, the number of couples, followed by the number of adulterous pairs, followed by the pairs, in the form "4h 2w" (husband from couple 4, wife from couple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n - 1 with the bride and groom being 0w and 0h.
Output
For each case, output a single line containing a list of the people that should be seated on the same side as the bride. If there are several solutions, any one will do. If there is no solution, output a line containing "bad luck".
Sample Input
10 6
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0
Sample Output
1h 2h 3w 4h 5h 6h 7h 8h 9h
Source
Waterloo Local Contest, 2007.9.29
题目大意:
有n对夫妇第i对夫妇表示为i-1h、i-1w。根据规定,每对夫妇必须坐在桌子的不同侧。现在有m对通~奸~关系,新娘(0号妻子)不想看到她对面的任意两个人有通奸关系。求一种满足的坐法并输出坐新娘同侧的人。
思路:
因为每对夫妻要么夫左妻右要么夫右妻左,所以很明显是一道2-SAT问题。
建图:设i'为第i个人的伴侣。对于某对通奸关系i和j,则连边i->j',j->i'。最后从新娘连一条边指向新郎以至于必须选新郎。
输出。。不说了
题目很6,同性通~奸~的设定也是醉了
源代码/pas:
type
edge=record
x,y,next:Longint;
end;
var
indeg,state,comp,dfn,low,opp,ls,ans,s:array[0..500]of longint;
v:array[0..500]of boolean;
e:array[0..2000000]of edge;
maxE,n,m,t:longint;
procedure add(x,y:longint);
begin
inc(maxE);
e[maxE].x:=x;
e[maxE].y:=y;
e[maxE].next:=ls[x];
ls[x]:=maxE;
end;
function nx(x:longint):longint;
begin
if x>n then
exit(x-n)
else
exit(x+n);
end;
function min(x,y:Longint):longint;
begin
if x<y then exit(x)
else exit(y);
end;
procedure init;
var
i,x,y:longint;
c:char;
s:string;
begin
fillchar(ls,sizeof(ls),0);
maxE:=0;
for i:=1 to m do
begin
read(c);
s:='';
while c in['0'..'9'] do
begin
s:=s+c;
read(c);
end;
val(s,x);
inc(x);
if c='w' then x:=x+n;
read(c);read(c);
s:='';
while c in['0'..'9'] do
begin
s:=s+c;
read(c);
end;
val(s,y);
inc(y);
if c='w' then y:=y+n;
readln;
add(x,nx(y));
add(y,nx(x));
end;
add(1+n,1);
end;
procedure tarjan(x:longint);
var
i:longint;
begin
inc(t);
dfn[x]:=t;
low[x]:=t;
inc(s[0]);
s[s[0]]:=x;
v[x]:=true;
i:=ls[x];
while i>0 do
with e[i] do
begin
if dfn[y]=0 then
begin
tarjan(y);
low[x]:=min(low[x],low[y]);
end
else
if v[y] then low[x]:=min(low[x],dfn[y]);
i:=next;
end;
if dfn[x]=low[x] then
begin
inc(comp[0]);
repeat
i:=s[s[0]];
comp[i]:=comp[0];
dec(s[0]);
v[i]:=false;
until x=i;
end;
end;
procedure sat;
var
i:longint;
begin
fillchar(dfn,sizeof(dfn),0);
fillchar(low,sizeof(low),0);
fillchar(v,sizeof(v),false);
s[0]:=0;comp[0]:=0;
t:=0;
for i:=1 to n*2 do
if dfn[i]=0 then tarjan(i);
end;
procedure topsort;
var
i,head,tail:longint;
begin
head:=0;
tail:=0;
for i:=1 to comp[0] do
if indeg[i]=0 then
begin
inc(tail);
state[tail]:=i;
end;
repeat
inc(head);
i:=ls[state[head]];
while i>0 do
with e[i] do
begin
dec(indeg[y]);
if indeg[y]=0 then
begin
inc(tail);
state[tail]:=y;
end;
i:=next;
end;
until head>=tail;
end;
procedure dfs_blue(x:longint);
var
i:longint;
begin
ans[x]:=-1;
i:=ls[x];
while i>0 do
with e[i] do
begin
if ans[y]=0 then dfs_blue(y);
i:=next;
end;
end;
procedure dfs_red;
var
i:longint;
begin
fillchar(ans,sizeof(ans),0);
for i:=1 to comp[0] do
if ans[state[i]]=0 then
begin
ans[state[i]]:=1;
dfs_blue(opp[state[i]]);
end;
end;
procedure print;
var
i:longint;
begin
for i:=2 to n do
begin
if i>2 then write(' ');
if ans[comp[i]]=-1 then
write(i-1,'h')
else
write(i-1,'w');
end;
writeln;
end;
procedure work;
var
i:Longint;
begin
for i:=1 to n do
begin
if comp[i]=comp[i+n] then
begin
writeln('bad luck');
exit;
end;
opp[comp[i]]:=comp[i+n];
opp[comp[i+n]]:=comp[i];
end;
fillchar(ls,sizeof(ls),0);
fillchar(indeg,sizeof(indeg),0);
for i:=1 to maxE do
with e[i] do
if comp[x]<>comp[y] then
begin
add(comp[y],comp[x]);
inc(indeg[comp[x]]);
end;
topsort;
dfs_red;
print;
end;
begin
readln(n,m);
while n+m>0 do
begin
init;
sat;
work;
readln(n,m);
end;
end.