luoguP4782 [模板]2-SAT问题
https://www.luogu.org/problemnew/show/P4782
2-SAT模板,输出方案只需判断 \(a\) 和 \(a + n\) 两个点所在的 scc 编号大小就可以了
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') fu = -1; c = getchar();}
while (c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
}
const int N = 2000000 + 10;
struct Edge {
int u, v, next;
}G[N << 1];
int head[N], col[N], low[N], dfn[N], st[N], inst[N];
int n, m, tot, cnt, Index, len;
inline void addedge(int u, int v) {
G[++tot] = (Edge) {u, v, head[u]}, head[u] = tot;
}
void tarjan(int u) {
low[u] = dfn[u] = ++Index;
st[++len] = u; inst[u] = 1;
for(int i = head[u]; i; i = G[i].next) {
int v = G[i].v;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(inst[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]) {
cnt++;
while(st[len + 1] != u) {
int tmp = st[len--];
col[tmp] = cnt;
inst[tmp] = 0;
}
}
}
int main() {
read(n); read(m);
for(int i = 1; i <= m; i++) {
int a, b, c, d;
read(a); read(b); read(c); read(d);
if(b == 0) {
if(d == 0) addedge(a + n, c), addedge(c + n, a);
else addedge(a + n, c + n), addedge(c, a);
} else {
if(d == 0) addedge(a, c), addedge(c + n, a + n);
else addedge(a, c + n), addedge(c, a + n);
}
}
for(int i = 1; i <= (n << 1); i++) if(!dfn[i]) tarjan(i);
for(int i = 1; i <= n; i++) if(col[i] == col[i + n]) {puts("IMPOSSIBLE"); return 0;}
puts("POSSIBLE");
for(int i = 1; i <= n; i++) if(col[i] > col[i + n]) printf("1 "); else printf("0 ");
return 0;
}
luoguP4782 [模板]2-SAT问题的更多相关文章
- 2 - sat 模板(自用)
2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一 POJ 3207 Ikki's Story IV ...
- LuoguP4782 【模板】2-SAT 问题 (2-SAT)
Not difficult, the only problem is how to deal with give 0/1 to the var. Tarjan offers the reverse t ...
- TwoSAT算法模板
该模板来自大白书 [解释] 给多个语句,每个语句为“ Xi为真(假) 或者 Xj为真(假)” 每个变量和拆成两个点 2*i为假, 2*i+1为真 “Xi为真 或 Xj为真” 等价于 “Xi为假 –& ...
- C++ 模板基础
我们学习使用C++,肯定都要了解模板这个概念.就我自己的理解,模板其实就是为复用而生,模板就是实现代码复用机制的一种工具,它可以实现类型参数化,即把类型定义为参数:进而实现了真正的代码可重用性.模版可 ...
- (模板)poj2947(高斯消元法解同余方程组)
题目链接:https://vjudge.net/problem/POJ-2947 题意:转换题意后就是已知m个同余方程,求n个变量. 思路: 值得学习的是这个模板里消元用到lcm的那一块.注意题目输出 ...
- 虚拟化技术之kvm镜像模板制作工具virt-sysprep
virt-sysprep这个工具来自libguest-tools这个工具包,它能够把kvm虚拟机对应的磁盘文件做成一个模板,后续我们启动虚拟机就可以基于这个镜像模板启动:什么是镜像模板呢?所谓模板就是 ...
- Jade模板引擎让你飞
写在前面:现在jade改名成pug了 一.安装 npm install jade 二.基本使用 1.简单使用 p hello jade! 渲染后: <p>hello jade!</p ...
- ABP入门系列(2)——通过模板创建MAP版本项目
一.从官网创建模板项目 进入官网下载模板项目 依次按下图选择: 输入验证码开始下载 下载提示: 二.启动项目 使用VS2015打开项目,还原Nuget包: 设置以Web结尾的项目,设置为启动项目: 打 ...
- CMS模板应用调研问卷
截止目前,已经有数十家网站与我们合作,进行了MIP化改造,在搜索结果页也能看到"闪电标"的出现.除了改造方面的问题,MIP项目组被问到最多的就是:我用了wordpress,我用了织 ...
随机推荐
- oracle 函数中,一定要注意出现空记录和多条记录的处理方法
今天折腾了3个小时,为一个以前不知道的oracle函数机制: 在sql查询中,如果一个查询未能获取记录,oracle不会报错 如select aa from bb where 1=2; 但在oracl ...
- python与桶排序
问题提出: 将以下数据: 6, 8, 2, 3, 4, 0, 9, 1, 5,1 按从小到达排列. 桶排序原理: 桶排序也叫计数排序,简单来说,就是将数据集里面所有元素按顺序列举出来,然后统计元素出现 ...
- PHP 取网页变量
$_POST["test"]; $_GET["test"];isset(); if(isset($_GET["yyuid"]))
- linux启动lcd屏如水纹状波动,不稳…
开发环境:arm-s3c2416.ubuntu. 内核:linux2.6.26 病症:内核启动时,arm的lcd屏幕出现抖动现象,如水纹状波动,屏幕最下面还有白线闪动,甚至lcd有很多亮点等现象 分析 ...
- arm-linux-gcc-4.5.1的安装…
原文地址:arm-linux-gcc-4.5.1的安装方法作者:游牧 说明:我使用的是在虚拟机下的红帽linux5 ,通过终端工具SecureCRT操作,使用虚拟机等其他工具的过程大致相同 1.使用r ...
- Bypassing iPhone Code Signatures
[Bypassing iPhone Code Signatures] Starting with the recent beta releases of the iPhoneOS, Apple has ...
- linux tcpdump补充
If they are going across the loopback interface, you may have to tell tcpdump to read that interface ...
- layer使用总结一配置
导入layer.js文件即可,必须先导入jquery.js文件,因为layer是基于jquery 版本匹配,在此记录一下,layer使用1.8下载时是2.3的版本,对应的jquery使用1.8.3版本 ...
- 使用python创建生成动态链接库dll
如今,随着深度学习的发展,python已经成为了深度学习研究中第一语言.绝大部分的深度学习工具包都有python的版本,很多重要算法都有python版本的实现.为了将这些算法应用到具体工程中,这些工具 ...
- Solidity notes
1. 查询transaction历史记录 https://forum.ethereum.org/discussion/2116/in-what-ways-can-storage-history-be- ...