【图论】【模板】2-SAT
2-SAT
定义
可以看一下洛谷模板题目的定义:
思路
每个等式都可以理解为如果 \(x\) 不是条件规定的,那么 \(y\) 必须按照条件规定的,反过来也一样。
所以我们将一个数字拆成两个点,对于每个条件将代表取反 \(x\) 的点与 \(y\) 相连,将代表取反 \(y\) 的点与 \(x\) 相连。
比如条件: \(x = 1\) 或 \(y = 0\),这说明如果 \(x\) 取 \(0\),那么 \(y\) 必取 \(0\),如果 \(y\) 取 \(1\),那么 \(x\) 必为 \(1\)。
然后跑 Tarjan 缩点,如果 \(x = 0\) 和 \(x = 1\) 在同一个强连通分量,那么找不到解,直接返回;否则,\(x = 0\) 和 \(x = 1\) 可能在一条链上,比如 \(x = 0\) 能推到 \(x = 1\),那么 \(x\) 只能取 \(1\),否则会引发错误;如果 \(x = 0\) 和 \(x = 1\) 完全无关,那么随便 \(x\) 取什么。
因为拓扑序在后面的点缩点对应的点 \(scc\) 的编号小,所以对于 P4782 这道模板题可以直接通过判断 \(scc\) 编号大小输出一组解。
代码
P4782 【模板】2-SAT 和 P3007 [USACO11JAN] The Continental Cowngress G 都是模板题。
P4782 代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
struct edge {
int to, next;
} e[N];
int head[N], idx = 1;
void add(int u, int v) {
idx++, e[idx].to = v, e[idx].next = head[u], head[u] = idx;
}
int n, m;
int dfn[N], low[N], cnt;
int stk[N], top;
bool instk[N];
int scc[N], tot;
void tarjan(int u) {
stk[++top] = u, instk[u] = 1;
dfn[u] = low[u] = ++cnt;
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (!dfn[to]) {
tarjan(to);
low[u] = min(low[u], low[to]);
}
else if (instk[to]) low[u] = min(low[u], dfn[to]);
}
if (dfn[u] == low[u]) {
tot++;
while (stk[top] != u) {
int t = stk[top];
top--;
instk[t] = 0;
scc[t] = tot;
}
top--;
instk[u] = 0;
scc[u] = tot;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int p1, x, p2, y;
cin >> p1 >> x >> p2 >> y;
add(p1 + (!x) * n, p2 + y * n);
add(p2 + (!y) * n, p1 + x * n);
}
for (int i = 1; i <= 2 * n; i++) if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++) {
if (scc[i] == scc[i + n]) {
cout << "IMPOSSIBLE\n";
return 0;
}
}
cout << "POSSIBLE\n";
for (int i = 1; i <= n; i++) {
if (scc[i + n] < scc[i]) cout << "1 ";
else cout << "0 ";
}
return 0;
}
P3007 要判断解是否唯一,那么看看边的连接情况即可得出(上面讲过了)。
P3007 代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 8010;
struct edge {
int to, next;
} e[M];
int head[N], idx = 1;
void add(int u, int v) {
idx++, e[idx].to = v, e[idx].next = head[u], head[u] = idx;
}
int n, m;
int dfn[N], low[N], cnt;
int stk[N], top;
bool instk[N];
int scc[N], tot;
void tarjan(int u) {
stk[++top] = u, instk[u] = 1;
dfn[u] = low[u] = ++cnt;
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (!dfn[to]) {
tarjan(to);
low[u] = min(low[u], low[to]);
}
else if (instk[to]) low[u] = min(low[u], dfn[to]);
}
if (dfn[u] == low[u]) {
tot++;
while (stk[top] != u) {
int t = stk[top];
top--;
instk[t] = 0;
scc[t] = tot;
}
top--;
instk[u] = 0;
scc[u] = tot;
}
}
bitset<N> connect[N];
bool vis[N];
void dfs(int u) {
if (vis[u]) return;
vis[u] = 1;
for (int i = 1; i <= tot; i++) {
if (connect[u][i]) {
dfs(i);
connect[u] |= connect[i];
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int p1, p2;
char x, y;
cin >> p1 >> x >> p2 >> y;
if (x == 'Y') x = 1; else x = 0;
if (y == 'Y') y = 1; else y = 0;
add(p1 + (!x) * n, p2 + y * n);
add(p2 + (!y) * n, p1 + x * n);
}
for (int i = 1; i <= 2 * n; i++) if (!dfn[i]) tarjan(i);
for (int i = 1; i <= n; i++) {
if (scc[i] == scc[i + n]) {
cout << "IMPOSSIBLE\n";
return 0;
}
}
for (int i = 1; i <= 2 * n; i++) {
for (int j = head[i]; j; j = e[j].next) {
int to = e[j].to;// i -> to
if (scc[i] != scc[to]) connect[scc[i]][scc[to]] = 1;
}
}
for (int i = 1; i <= tot; i++) connect[i][i] = 1;
for (int i = 1; i <= tot; i++) if (!vis[i]) dfs(i);
for (int i = 1; i <= n; i++) {
if (connect[scc[i]][scc[i + n]]) cout << "Y";
else if (connect[scc[i + n]][scc[i]]) cout << "N";
else cout << "?";
}
return 0;
}
例题
MyBlog: P5782 [POI2001] 和平委员会