poj3648

Wedding
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8884 Accepted: 2675 Special Judge

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
 
 
题目大意:该题很难理解。有一对新婚夫妇做在一张很长的桌子两边。有n-1对夫妻陪伴,这n-1对夫妻也是分坐在桌子两端的。然后,这些人之间有不正当
 把丈夫和新娘同一边看做点i,在新娘对面看做i+n。然后根据通奸关系进行构图。因为新郎也有可能有通奸关系,
 所以构图时加入新郎点0,增加一条边,0-->0+n。然后就是求解2-SAT问题了。
然而到现在我还没AC,唉。

WA代码:
var
  a:array [0..101,0..101] of longint;
  v,f:array [0..1010] of boolean;
  low,dfn,st,belong,c:Array [0..2010] of longint;
  i,j,m,n,x,y,p,ii,len,s1,d,t,k:longint;
  ch1,ch2:char;
  fff:boolean;
  s:string;


procedure add(x,y:longint);
begin
  inc(c[x]);
  a[x,c[x]]:=y;
end;


procedure output1;
begin
  for i:=1 to n-1 do
  begin
    if i<>n-1 then
    begin
      if belong[i]<belong[i+n] then
      write(i,'h ') else
      write(i,'w ');
    end
    else
    if belong[i]<belong[i+n] then
      write(i,'h') else
      write(i,'w');
  end;
end;


procedure input1;
begin
  readln(n,m);
  x:=1;
  y:=1;
  while (x<>0) and (y<>0) do
  begin
    readln(s);
    x:=0;
    y:=0;
    ii:=0;
    j:=0;
    for j:=1 to length(s) do
    begin
      if (s[j]<'0') or (s[j]>'9') then
      break;
      x:=x*10+ord(s[j])-ord('0');
    end;
    ch1:=s[j];
    for ii:=j+2 to length(s) do
    begin
      if (s[ii]<'0') or (s[ii]>'9') then
      break;
      y:=y*10+ord(s[ii])-ord('0');
    end;
    ch2:=s[ii];
    if (ch1='h') and (ch2='h') then
    begin
      add(y+n,x);
      add(x+n,y);
    end;
    if (ch1='h') and (ch2='w') then
    begin
      add(x+n,y+n);
      add(y,x);
    end;
    if (ch1='w') and (ch2='h') then
    begin
      add(x,y);
      add(y+n,x+n);
    end;
    if (ch1='w') and (ch2='w') then
    begin
      add(x,y+n);
      add(y,x+n);
    end;
  end;
end;


function pp:longint;
begin
  f[st[t]]:=false;
  dec(t);
  exit(st[t+1]);
end;


procedure check1;
var
  i:longint;
begin
  for i:=1 to n do
  if belong[i]=belong[n+i] then
  begin
    fff:=false;
    exit;
  end;
end;


function min(x,y:longint):longint;
begin
  if x>y then
  exit(y);
  exit(x);
end;


procedure tarjan(x:longint);
var
  i,y:longint;
begin
  inc(d);
  low[x]:=d;
  dfn[x]:=d;
  inc(t);
  st[t]:=x;
  f[x]:=true;
  for i:=1 to c[x] do
  begin
    if not v[a[x,i]] then
    begin
      v[a[x,i]]:=true;
      tarjan(a[x,i]);
      low[x]:=min(low[x],low[a[x,i]]);
    end else
    begin
      if f[a[x,i]] then
      low[x]:=min(low[x],dfn[a[x,i]]);
    end;
  end;
  if dfn[x]=low[x] then
  begin
    inc(p);
    belong[x]:=p;
    y:=x-1;
    while x<>y do
    begin
      y:=pp;
      belong[y]:=p;
    end;
  end;
end;




begin


  input1;
  add(0,n);
  for i:=1 to 2*n do
  if not v[i] then
  begin
    v[i]:=true;
    tarjan(i);
  end;
  fff:=true;
  check1;
  if not fff then
  begin
    writeln('bad luck');
    halt;
  end;
  output1;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值