题意:新娘新郎和每对夫妇,都分开坐,新娘不希望看到对面有特殊关系的一对人
思路:可以考虑选一些人坐在新娘对面(也就是新郎这一边),新郎必须选(建边 新娘->新郎),有特殊关系的每一对人建边,反着输出新娘这一边的人
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 1005;
struct edge
{
int to,next;
bool cut;
}ed[maxn];
int head[maxn], tot;
int Low[maxn], DFN[maxn], Stack[maxn];
int Index, top;
bool Instack[maxn];
int block,belong[maxn];
int bridge;
void init()
{
memset(head, -1, sizeof(head));
memset(DFN, 0, sizeof(DFN));
memset(Instack, false, sizeof(Instack));
block = tot = 0;
Index = top = 0;
bridge = 0;
}
void Tarjan(int u,int pre)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u];i != -1;i = ed[i].next)
{
v = ed[i].to;
//if(v == pre)continue; //无向无重边 用,只要不往回走
//if(ed[i].id == pre) continue; //无向图重边判定,不走同一条边
if( !DFN[v] )
{
Tarjan(v,u);
//Tarjan(v,ed[i].id); //无向图重边判定
if( Low[u] > Low[v] )Low[u] = Low[v];
if(Low[v] > DFN[u])
{
bridge++;
ed[i].cut = true;
ed[i^1].cut = true;
}
}
else if( Instack[v] && Low[u] > DFN[v] )
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
block++;
do
{
v = Stack[--top];
Instack[v] = false;
belong[v] = block;
}
while( v!=u );
}
}//使无向图双连通,(叶子+1) / 2
void add(int x,int y)
{
ed[tot].to = y;
ed[tot].next = head[x];
ed[tot].cut = 0;
//ed[tot].id = num; //无向图重边判定
head[x] = tot++;
}
void SCC(int n)
{
for(int i = 0; i < n; i++)
if(!DFN[i])
Tarjan(i,-1);
}
int bian(int a,char c,int n)
{
if(c == 'w')
return a;
else
return a+n;
}
int fan(int a,char c,int n)
{
if(c == 'w')
return a+n;
else
return a;
}
int main(void)
{
int n,m,a,b;
char c,d;
while(scanf("%d%d",&n,&m)!=EOF && n+m)
{
init();
add(0,n);
while(m--)
{
scanf("%d%c%d%c",&a,&c,&b,&d);
add(bian(a,c,n),fan(b,d,n));
add(bian(b,d,n),fan(a,c,n));
}
SCC(2*n);
int flag = 1;
for(int i = 1; i < n; i++)
{
if(belong[i] == belong[i+n])
{
flag = 0;
break;
}
}
if(!flag)
{
printf("bad luck\n");
continue;
}
for(int i = 1; i < n; i++)
{
if(belong[i] > belong[i+n])
printf("%dw%c",i,i==n-1?'\n':' ');
else
printf("%dh%c",i,i==n-1?'\n':' ');
}
}
return 0;
}