题目
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.
输入格式
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.
输出格式
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".
输入样例
10 6
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0
输出样例
1h 2h 3w 4h 5h 6h 7h 8h 9h
题解
题意:有n对夫妻坐成两排,其中0号为新娘新郎。有m对奸情,新娘不愿意看到对面排存在奸情,问合理方案
每对夫妻只有两种坐法,要么和新郎一排,要么和新娘一排
每对奸情不能同时在新郎一排
为了方便,我们选择以是否和新郎一排座位标准
建图:
首先新娘向新郎连边,表示新娘不可能和新郎一排
对于每对奸情a和b,我们设op[a]表示a的另一半
那么a->op[b],b->op[a],即a在新郎这侧则b的另一半必须在新郎这侧,b同理
按照我们之前的论断,Scc编号较小的被选择和新郎坐
那么另一半就和新娘坐
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<‘ ‘; puts("");
using namespace std;
const int maxn = 1005,maxm = 1000005,INF = 1000000000;
char opt;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == ‘-‘) flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - ‘0‘; c = getchar();}
opt = c;
return out * flag;
}
int n,m,h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
if (!dfn[to = ed[k].to]){
dfs(to);
low[u] = min(low[u],low[to]);
}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do{Scc[st[top]] = scci;}while (st[top--] != u);
}
}
void init(){
memset(h,0,sizeof(h));
memset(Scc,0,sizeof(Scc));
memset(dfn,0,sizeof(dfn));
cnt = scci = top = 0; ne = 1;
}
int main(){
while (~scanf("%d%d",&n,&m)){
if (!n && !m) break;
init();
int a,b;
build(1,0);
while (m--){
a = 2 * read(); if (opt == ‘w‘) a ^= 1;
b = 2 * read(); if (opt == ‘w‘) b ^= 1;
build(a,b ^ 1); build(b,a ^ 1);
}
for (int i = 0; i < (n << 1); i++) if (!dfn[i]) dfs(i);
bool flag = true;
for (int i = 0; i < n; i++)
if (Scc[i << 1] == Scc[i << 1 | 1]){
flag = false; break;
}
if (!flag) puts("bad luck");
else {
for (int i = 1; i < n; i++)
if (Scc[i << 1] > Scc[i << 1 | 1]) printf("%dh ",i);
else printf("%dw ",i);
puts("");
}
}
return 0;
}