POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)

时间:2022-09-18 23:34:09
Wedding
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10427   Accepted: 3170   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

题目链接:POJ 3648

题目讲的不是很清楚,实际上如下图所示

POJ 3648 Wedding(2-SAT的模型运用+DFS | Tarjan)

其中0号新婚夫妻是固定的了即0w在左0h在右,这题我是先把每一个夫妻拆成妻子和丈夫,然后两者均有两种状态,妻子在左或右,丈夫在左或右,因此可以拆成4*N个点,然后记在左符号为无,在右为$\lnot$,建图后跑2-SAT,首先由于固定的0号夫妻,因此0w在左、0h在右的情况必选,根据离散数学的公式可以得到有向边$<\lnot 0w, 0w>与<0h, \lnot 0h>$,然后然后任意一对夫妻不能坐在同一侧,有:$\lnot (a \land b) \land \lnot (\lnot a \land \lnot b)$,这样得到四条有向边:$<a,\lnot b><b, \lnot a><\lnot a, b><\lnot b, a>$

再然后根据每一对通奸的a与b,显然a与b不能同时出现在0w的右侧,即$\lnot (\lnot a \land \lnot b)=a \lor b$,因此得到两条有向边$<\lnot a, b>$与$<\lnot b, a>$,建图跑2-SAT后由于0w必选,因此mark[0w]是为1的,又由于mark为1的点在方案内,因此把mark为1的点都选出来并判断输出即可。

DFS输出最小字典序方案的代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen("name","r",stdin)
#define fout(name) freopen("name","w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 35 << 2;
const int M = 60 * 60 * 2 + 2 + 4 * 35;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[M];
int head[N], tot;
int st[N], top;
int vis[N];
int n, m; void init()
{
CLR(head, -1);
tot = 0;
CLR(vis, 0);
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
int rev(int i)
{
return i ^ 1;
}
int dfs(int u)
{
if (vis[rev(u)])
return 0;
if (vis[u])
return 1;
vis[u] = 1;
st[top++] = u;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!dfs(v))
return 0;
}
return 1;
}
int check(int n)
{
for (int i = 0; i < (n << 1); i += 2)
{
top = 0;
if (!vis[i] && !vis[rev(i)] && !dfs(i))
{
while (top)
vis[st[--top]] = 0;
if (!dfs(rev(i)))
return 0;
}
}
return 1;
} int main(void)
{
int i;
while (~scanf("%d%d", &n, &m) && (n || m))
{
init();
add(rev(0 << 2), 0 << 2);
add((0 << 2) + 2, rev((0 << 2) + 2));
for (i = 1; i < n; ++i) //4n
{
add(4 * i, rev(4 * i + 2));
add(4 * i + 2, rev(4 * i));
add(rev(4 * i), 4 * i + 2);
add(rev(4 * i + 2), 4 * i);
}
int u, v;
char su, sv;
for (i = 0; i < m; ++i) //2m
{
scanf(" %d%c %d%c", &u, &su, &v, &sv);
u <<= 2;
v <<= 2;
if (su == 'h')
u += 2;
if (sv == 'h')
v += 2;
add(rev(u), v);
add(rev(v), u);
}
if (!check(n << 1))
puts("bad luck");
else
{
for (int i = 4; i < (n << 2); i += 4)
printf("%d%c ", i >> 2, vis[i] ? 'w' : 'h');
puts("");
}
}
return 0;
}

Tarjan代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen("name","r",stdin)
#define fout(name) freopen("name","w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 35 << 2;
const int M = 60 * 60 * 2 + 2 + 4 * 35;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[M];
int head[N], tot;
int dfn[N], low[N], belong[N], st[N], top, ts, sc;
int ins[N];
int n, m; void init()
{
CLR(head, -1);
tot = 0;
CLR(dfn, 0);
CLR(low, 0);
CLR(belong, 0);
top = ts = sc = 0;
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
int rev(int i)
{
return i ^ 1;
}
void scc(int u)
{
dfn[u] = low[u] = ++ts;
ins[u] = 1;
st[top++] = u;
int i, v;
for (i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!dfn[v])
{
scc(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u])
{
++sc;
do
{
v = st[--top];
ins[v] = 0;
belong[v] = sc;
} while (u != v);
}
}
int main(void)
{
int i;
while (~scanf("%d%d", &n, &m) && (n || m))
{
init();
add(rev(0 << 2), 0 << 2);
add((0 << 2) + 2, rev((0 << 2) + 2));
for (i = 1; i < n; ++i) //4n
{
add(4 * i, rev(4 * i + 2));
add(4 * i + 2, rev(4 * i));
add(rev(4 * i), 4 * i + 2);
add(rev(4 * i + 2), 4 * i);
}
int u, v;
char su, sv;
for (i = 0; i < m; ++i) //2m
{
scanf(" %d%c %d%c", &u, &su, &v, &sv);
u <<= 2;
v <<= 2;
if (su == 'h')
u += 2;
if (sv == 'h')
v += 2;
add(rev(u), v);
add(rev(v), u);
}
for (i = 0; i < (n << 2); ++i)
if (!dfn[i])
scc(i);
int flag = 1;
for (i = 0; i < (n << 2) && flag; i += 2)
if (belong[i] == belong[i ^ 1])
flag = 0;
if (!flag)
puts("bad luck");
else
{
for (int i = 4; i < (n << 2); i += 4)
printf("%d%c ", i >> 2, belong[i] < belong[i ^ 1] ? 'w' : 'h');
puts("");
}
}
return 0;
}