Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 9553 | Accepted: 2901 | 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
刷这个题,就是为了熟悉的2-SAT算法。
2-SAT算法:
1、首先根据题意建模型。
2、然后用Tarjan算法找连通块,其后就是对每一个连通块进行操作。
3、然后从影响最小的连通块开始,实际上就是从连通块的拓扑排序的从底向上顺序开始标记。拓扑排序的从底向上的顺序相当于此图的反图的拓扑序。
4、由于2-SAT图的具有对称性,若i选择了,则 ’i 就不能选择了,并与之连接的点都不能选择了(实际上,是 ‘i 的先代节点)。染色便可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 100+10;
int n, m;
vector<int>p[maxn];
vector<int>ma[maxn];
int vis[maxn];
int stack[maxn];
int ind[maxn];
int low[maxn];
int dfn[maxn];
int id[maxn];
int sig, sk, lay;
char color[maxn];
queue<int>q1;
queue<int>q2;
void init() {
memset(dfn,0, sizeof(dfn));
memset(color, 0, sizeof(color));
memset(vis, 0, sizeof(vis));
memset(low, 0, sizeof(low));
memset(ind, 0, sizeof(ind));
memset(stack, 0, sizeof(stack));
memset(id, 0, sizeof(id));
sig = 0;
lay = 0;
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
for (int i=0; i<2*n; i++)
p[i].clear(), ma[i].clear();
sk = 0;
}
void Tarjan(int u) {
vis[u] = 1;
dfn[u] = low[u] = lay++;
stack[++sk] = u;
for (int i=0; i<p[u].size(); i++) {
if (vis[p[u][i]] == 0)
Tarjan(p[u][i]);
if (vis[p[u][i]] == 1) //注意这儿。
low[u] = min(low[u], low[p[u][i]]);
}
if (dfn[u] == low[u]) {
sig++;
do {
id[stack[sk]] = sig;
vis[stack[sk]] = 2; //注意这儿。
}while (stack[sk--] != u);
}
}
bool ok() {
for (int i=0; i<2*n; i+=2)
if (id[i] == id[i^1])
return false;
return true;
}
void topsort() {
int now;
while (!q1.empty()) {
now = q1.front();
q1.pop();
if (color[now] != 0)
continue;
color[now] = 'R';
ind[now] = -1;
for (int i=0; i<2*n; i++) {
if (now == id[i]) {
q2.push(id[(i^1)]);
while (!q2.empty()) {
int pre = q2.front();
q2.pop();
if (color[pre] == 'B')
continue;
color[pre] = 'B';
for (int j=0; j<ma[pre].size(); j++)
q2.push(ma[pre][j]);
}
}
}
for (int i=0; i<ma[now].size(); i++) {
ind[ma[now][i]]--;
if (ind[ma[now][i]] == 0)
q1.push(ma[now][i]);
}
}
}
int main(){
while (scanf("%d%d", &n, &m)!=EOF) {
init();
if (n == 0 && m == 0)
break;
for (int i=0; i<m; i++) {
int x, y;
char c1, c2;
scanf("%d%c%d%c", &x, &c1, &y, &c2);
int u, v;
if (c1 == 'w')
u = 2*x;
else
u = 2*x+1;
if (c2 == 'w')
v = 2*y;
else
v = 2*y+1;
if (u != (v^1)) {
p[u].push_back(v^1);
p[v].push_back(u^1);
}
}
p[0].push_back(1);
for (int i=0; i<2*n; i++)
if (!vis[i])
Tarjan(i);
if (!ok()) { // 可行性的判断。
puts("bad luck");
continue;
}
for (int i=0; i<2*n; i++) {
for (int j=0; j<p[i].size(); j++)
if (id[i] != id[p[i][j]]) { //不同连通块
ma[id[p[i][j]]].push_back(id[i]); //连通块的反图
ind[id[i]]++; //记录连通块的入度。
}
}
for(int i=1; i<=sig; i++)
if (ind[i] == 0)
q1.push(i);
topsort(); //染色。
for (int i=1; i<n; i++) {
if (i > 1)
printf(" ");
if (color[id[2*i]] == 'R')
printf("%dh", i);
else
printf("%dw", i);
}
cout << endl;
}
return 0;
}