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;
}

最新文章

  1. Codeforces Round #366 (Div. 2)
  2. 【py技巧】使用reload重导入修改过的包或模块
  3. 一个封装好的CSV文件操作C#类代码
  4. android studio修改新项目package名称
  5. Chapter15:派生类
  6. solrj:org.apache.solr.common.util.NamedList.java
  7. select * from table where 1=1
  8. xml增强学习笔记
  9. BZOJ1631: [Usaco2007 Feb]Cow Party
  10. jquery 节点操作大全
  11. Calendar 日历控件使用
  12. this笔记
  13. .net mvc web api 返回 json 内容时过滤值为null的属性
  14. Spark RPC框架源码分析(三)Spark心跳机制分析
  15. python - 列表,元组
  16. JSJ—类与对象
  17. redis设置防火墙的问题
  18. Linux修改日期、时间,系统与硬件时间
  19. Qt Model/View学习(二)
  20. [转]C#使用 Salt + Hash 来为密码加密

热门文章

  1. swift(Object Storage对象存储服务)(单节点)
  2. js取得前2位字符
  3. Functions &amp; Closures
  4. jquery中的cookie使用
  5. 476. Number Complement 二进制中的相反对应数
  6. Python读取CSV文件,报错:UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0xa7 in position 727: illegal multibyte sequence
  7. 面试题:缓存Redis与Memcached的比较 有用
  8. oracle高级查询练习题
  9. 31.SUM() 函数
  10. rpm遇到的坑-与VMP冲突