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
这道题恶心了我很久,学车的时候都在想。。。。
首先,每对夫妇一定坐在2侧,这是建图的条件之一,之二是根据输入所给的通奸关系,之三,也是最容易漏的,我们可以假设新娘坐一侧,而且只能坐在那一侧,新郎也一样,因此,这里就存在一个必选关系,对于点对(i,i'),假设必定选i,则要添加边 i'->i
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=4010;
const int M=N*N;
struct node
{
int from;
int to;
int next;
}edge[M],redge[M];
int head[N],rhead[N];
bool instack[N];
int DFN[N],block[N],color[N],in_deg[N],low[N],Stack[N],cfl[N];
int index,tot,sccnum,Top,rtot,n,m;
void addedge(int from,int to)
{
edge[tot].from=from;
edge[tot].to=to;
edge[tot].next=head[from];
head[from]=tot++;
}
void raddedge(int from,int to)
{
redge[rtot].from=from;
redge[rtot].to=to;
redge[rtot].next=rhead[from];
rhead[from]=rtot++;
}
void tarjan(int u)
{
DFN[u]=low[u]=++index;
instack[u]=1;
Stack[Top++]=u;
for (int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if (DFN[v]==-1)
{
tarjan(v);
if(low[u]>low[v])
low[u]=low[v];
}
else if (instack[v] && low[u]>DFN[v])
low[u]=DFN[v];
}
if (DFN[u]==low[u])
{
int v;
sccnum++;
do
{
Top--;
v=Stack[Top];
block[v]=sccnum;
instack[v]=0;
}
while (v!=u);
}
}
void topo_sort()
{
queue<int>qu;
memset(color,0,sizeof(color));
for (int i=1;i<=sccnum;i++)
if (in_deg[i]==0)
qu.push(i);
while (!qu.empty())
{
int u=qu.front();
qu.pop();
if (!color[u])
{
color[u]=1;
color[cfl[u]]=-1;
}
for (int i=rhead[u];i!=-1;i=redge[i].next)
{
int v=redge[i].to;
in_deg[v]--;
if (!in_deg[v])
qu.push(v);
}
}
}
bool judge()
{
for (int i=0;i<2*n;i++)
{
if (block[2*i]==block[2*i+1])
return false;
cfl[block[2*i]]=block[2*i+1];
cfl[block[2*i+1]]=block[2*i];
}
return true;
}
void init()
{
memset(block,-1,sizeof(block));
memset(DFN,-1,sizeof(DFN));
memset(instack,0,sizeof(instack));
index=sccnum=Top=0;
}
void build()
{
memset(head,-1,sizeof(head));
tot=0;
int a,b;
char s1,s2;
addedge(1,0);
addedge(2,3);
for (int i=1;i<n;i++)
{
addedge(4*i,4*i+3);
addedge(4*i+3,4*i);
addedge(4*i+1,4*i+2);
addedge(4*i+2,4*i+1);
}
for (int i=1;i<=m;i++)
{
scanf("%d%c %d%c",&a,&s1,&b,&s2);
a=(s1=='w')?(4*a):(4*a+2);
b=(s2=='w')?(4*b):(4*b+2);
addedge(a+1,b);
addedge(b+1,a);
}
}
void r_build()
{
memset(in_deg,0,sizeof(in_deg));
memset(rhead,-1,sizeof(rhead));
rtot=0;
for (int i=0;i<tot;i++)
{
int u=edge[i].from;
int v=edge[i].to;
if (block[u]!=block[v])
{
raddedge(block[v],block[u]);
in_deg[block[u]]++;
}
}
}
void solve()
{
build();
init();
for (int i=0;i<4*n;i++)
if (DFN[i]==-1)
tarjan(i);
if (!judge())
printf("bad luck\n");
else
{
bool flag=false;
r_build();
topo_sort();
for (int i=1;i<n;i++)
{
if (!flag)
flag=1;
else
printf(" ");
if (color[block[4*i+2]]==1)
printf("%dh",i);
else
printf("%dw",i);
}
printf("\n");
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
if (n==0 && m==0)
break;
solve();
}
return 0;
}