uva 11294

Problem E: Wedding

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".

Sample Input

10 6
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0

Possible Output for Sample Input

1h 2h 3w 4h 5h 6h 7h 8h 9h

2-sat
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector> using namespace std; const int MAX_N = ;
int N,M;
int low[ * MAX_N],pre[ * MAX_N],cmp[ * MAX_N];
vector<int> G[ * MAX_N];
stack<int> S;
int dfs_clock,scc_cnt;
int ans[MAX_N * ]; void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u],low[v]);
} else if(!cmp[v]) {
low[u] = min(low[u],pre[v]);
}
} if(pre[u] == low[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} bool scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i < * N; ++i) if(!pre[i]) dfs(i); for(int i = ; i < * N; ++i) {
if(cmp[i] == cmp[ * N + i]) return false;
} return true;
} int main()
{
// freopen("sw.in","r",stdin);
while(~scanf("%d%d",&N,&M) && (N || M)) {
for(int i = ; i < * N; ++i) G[i].clear();
for(int i = ; i < N; ++i) {
G[i].push_back(i + N + * N);
G[i + * N].push_back(i + N);
G[i + N].push_back(i + * N);
G[i + * N].push_back(i);
}
G[ * N + N].push_back(N);
G[].push_back( * N); for(int i = ; i <= M; ++i) {
int u,v;
char ch1,ch2;
scanf("%d%c%d%c",&u,&ch1,&v,&ch2);
u += ch1 == 'h' ? N : ;
v += ch2 == 'h' ? N : ;
G[u].push_back(v + * N);
G[v].push_back(u + * N); } if(!scc()) {
printf("bad luck\n");
} else {
int len = ;
for(int i = ; i < N; ++i) {
if(cmp[i + * N] < cmp[i]) {
ans[len++] = i;
}
if(cmp[i + N + * N] < cmp[i + N]) {
ans[len++] = i + N;
}
} for(int i = ; i < len; ++i) {
printf("%d%c",ans[i] >= N ? ans[i] - N : ans[i],
ans[i] >= N ? 'h' : 'w');
printf("%c",i == len - ? '\n' : ' ' );
}
}
}
//cout << "Hello world!" << endl;
return ;
}
上一篇:go web framework gin group api 设计


下一篇:UIWindow & UIWindowLevel