当前位置:网站首页>2-SAT
2-SAT
2022-08-08 06:14:00 【lovesickman】
2-SAT
给 n n n 个命题,Each proposition has only two variables,Each variable is either 1 1 1(真) 要么是 0 0 0(假).
Ask if there are valid constructs such that the AND of all propositions holds.
- a → b * ¬ a ∨ b a \rightarrow b \iff \neg a \vee b a→b*¬a∨b ( → \rightarrow → is implication,若 p 则 q的意思)
- a → b * ¬ b → ¬ a a \rightarrow b \iff \neg b \rightarrow \neg a a→b*¬b→¬a (原命题和逆否命题等价)
- x x x 和 ¬ x \neg x ¬x 在同一个 SCC 中 无解.(3is a necessary condition for a solution)
- If the above conditions are met,There must be a solution?(证明充分性)
- 答案:一定有.
- 发现,如果有 a → x 1 → x 2 → . . . → b a \rightarrow x_1 \rightarrow x_2 \rightarrow ... \rightarrow b a→x1→x2→...→b , b → y 1 → y 2 → . . . → a b \rightarrow y_1 \rightarrow y_2 \rightarrow ... \rightarrow a b→y1→y2→...→a .Then according to the inverse negative proposition there must be: ¬ a ← x 1 ← x 2 ← . . . ← ¬ b \neg a \leftarrow x_1 \leftarrow x_2 \leftarrow ... \leftarrow \neg b ¬a←x1←x2←...←¬b , ¬ b ← y 1 ← y 2 ← . . . ← ¬ a \neg b \leftarrow y_1 \leftarrow y_2 \leftarrow ... \leftarrow \neg a ¬b←y1←y2←...←¬a .所以如果 a , b a,b a,b 在同一个 SCC 中, ¬ a , ¬ b \neg a ,\neg b ¬a,¬b Must be the same SCC 中.
- 每个 SCC The variables in either are selected,要么都不选.每个SCCThere must be a counterpart to himSCC.
- 构造方式:先 tarjan 缩点,进行拓扑排序,For both cases of a variable,Take the one that is further back.
Summary of propositional conditions:
- a ∨ b * ¬ a → b * ¬ b → a a \vee b \iff \neg a \rightarrow b \iff \neg b \rightarrow a a∨b*¬a→b*¬b→a
- a → b * ¬ a ∨ b * ¬ b ∨ a a \rightarrow b \iff \neg a \vee b \iff \neg b \vee a a→b*¬a∨b*¬b∨a
- a = 1 * a ∨ a a = 1 \iff a \vee a a=1*a∨a
- a = 0 * ¬ a ∨ ¬ a a = 0 \iff \neg a \vee \neg a a=0*¬a∨¬a
way of connecting:
- i 对应 i+n , in the template title:
while (m -- )
{
int i, a, j, b;
scanf("%d%d%d%d", &i, &a, &j, &b);
// i,i+n ; j,j+n
add(i + n * (a & 1), j + n * (b ^ 1));
add(j + n * (b & 1), i + n * (a ^ 1));
}
- 2 * i 为真,2 * i + 1是假
add(2 * i + !a, 2 * j + b);
add(2 * j + !b, 2 * i + a);
因为tarjan得到的idis topological inversion,Finally, the forward order traversal will do.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 2000010, M = 2000010;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], ts, stk[N], top;
int id[N], cnt;
bool ins[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++ ts;
stk[ ++ top] = u, ins[u] = true;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (!dfn[j])
{
tarjan(j);
low[u] = min(low[u], low[j]);
} else if (ins[j]) low[u] = min(low[u], dfn[j]);
}
if (low[u] == dfn[u])
{
int y;
cnt ++ ;
do
{
y = stk[top -- ], ins[y] = false, id[y] = cnt;
} while (y != u);
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int i, a, j, b;
scanf("%d%d%d%d", &i, &a, &j, &b);
// i,i+n ; j,j+n
add(i + n * (a & 1), j + n * (b ^ 1));
add(j + n * (b & 1), i + n * (a ^ 1));
}
for (int i = 1; i <= n * 2; i ++ )
if (!dfn[i])
tarjan(i);
for (int i = 1; i <= n; i ++ )
if (id[i] == id[i + n])
{
puts("IMPOSSIBLE");
return 0;
}
puts("POSSIBLE");
for (int i = 1; i <= n; i ++ )
if (id[i] < id[i + n]) printf("1 ");
else printf("0 ");
return 0;
}
边栏推荐
- convolutional neural network image recognition, convolutional neural network image processing
- Style of DataGrid in wpf
- Sqlmap + dnslog injection of repetition
- selenium模拟登录某宝
- 云计算和云服务,云计算
- Go-Excelize API source code reading (10) - SetActiveSheet(index int)
- Unity鼠标光标使用学习
- 字符串哈希 哈希值
- 28. Anomaly detection
- APISIX Ingress v1.5-rc1 released
猜你喜欢

oracle的插入sql错误

The amount of parameters and calculation of neural network, is the neural network a parametric model?

postman---postman parameterization

apifox使用文档之环境变量 / 全局变量 / 临时变量附apifox学习路线图

Horizontal version of the generated image uniapp H5 signature

自动化工具

基本工具-NETCAT(telnet-banner、传输文本信息)

convolutional neural network image recognition, convolutional neural network image processing

Why do big Internet companies keep hiring while frantically laying off staff?

Style of DataGrid in wpf
随机推荐
字符串哈希 哈希值
Integer block sample
使用 Zap 和 W3af 进行 Web 应用程序漏洞评估
PAT乙级-B1029 旧键盘(20)
仿QQ好友列表,QListWidget!
时钟的同步与异步问题
【ESP8266】ESP12S/12F 最小系统设计及typeC自动下载电路设计
navicat15 连接Oracle数据库 报错ORA-28547: connection to server failed, probable Oracle Net admin error的解决方案
【RPC】Mercury RPC
Preprocessing Notes
KDD'22 Recommendation System Papers (24 Research & 36 Application Papers)
Summary of digital IC design written test questions (4): some basic knowledge points
【分布式】链路追踪 jaeger
为什么互联网大厂一边疯狂裁员,一边不停招聘?
梅科尔工作室-深度学习-BP神经网络
Leetcode sword 】 refers to the Offer (special commando) summary
76. The minimum cover substring
cnn卷积神经网络反向传播,卷积神经网络维度变化
MySQL6
数字IC设计笔试题汇总(四):一些基础知识点