BZOJ 3495 [2-SAT]
来源:互联网 发布:法国制造业年度数据 编辑:程序博客网 时间:2024/06/07 00:21
Description
Solution
经典二元关系 2-SAT 的模型,但是关系数是 O(n 2 ) 的,不
能承受。
对于一个有 y 个城市的国家,新建 2y 个变量,分别表示前
i 个城市和后 i 个城市中是否有首都。
如此一来关系数降至 O(n + m)。
时间复杂度 O(n + m)。
by Claris
#include <cstdio>#include <iostream>using namespace std;const int N = 6010101;inline char get(void) { static char buf[100000], *S = buf, *T = buf; if (S == T) { T = (S = buf) + fread(buf, 1, 100000, stdin); if (S == T) return EOF; } return *S++;}inline void read(int &x) { static char c; x = 0; int sgn = 0; for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1; for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0'; if (sgn) x = -x;}int k, n, m, Gcnt, x, y, p, Scnt, cnt, dlc, top;int c[N];int S[N];struct edge { int to, next; edge(int t = 0, int n = 0):to(t), next(n) {}};edge G[N * 2];int head[N], pre[N], low[N], scc[N];inline void AddEdge(int from, int to) { G[++Gcnt] = edge(to, head[from]); head[from] = Gcnt;}inline int Min(int a, int b) { return a < b ? a : b;}void dfs(int u) { pre[u] = low[u] = ++dlc; int to; S[++top] = u; for (int i = head[u]; i; i = G[i].next) { to = G[i].to; if (!pre[to]) { dfs(to); low[u] = Min(low[u], low[to]); } else if (!scc[to]) { low[u] = Min(low[u], pre[to]); } } if (low[u] == pre[u]) { Scnt++; int x; while (true) { x = S[top]; top--; scc[x] = Scnt; if (x == u) break; } }}bool Check(void) { int lim = cnt << 1 | 1; for (int i = 1; i <= lim; i++) if (!pre[i]) dfs(i); for (int i = 1; i <= cnt; i++) if (scc[i << 1] == scc[i << 1 | 1]) return false; return true;}int main(void) { read(n); read(m); read(k); for (int i = 0; i < m; i++) { read(x); read(y); AddEdge(x << 1, y << 1 | 1); AddEdge(y << 1, x << 1 | 1); } cnt = n; while (k--) { read(p); for (int i = 1; i <= p; i++) { read(x); y = cnt + i; if (i != 1) { AddEdge((y - 1) << 1 | 1, y << 1 | 1); AddEdge(y << 1, (y - 1) << 1); AddEdge(x << 1 | 1, (y - 1) << 1); } else { AddEdge(x << 1, y << 1); } AddEdge(y << 1, x << 1); AddEdge(x << 1 | 1, y << 1 | 1); y = cnt + i + p; if (i != p) { AddEdge((y + 1) << 1 | 1, y << 1 | 1); AddEdge(y << 1, (y + 1) << 1); AddEdge(x << 1 | 1, (y + 1) << 1); } else { AddEdge(x << 1, y << 1); } AddEdge(y << 1, x << 1); AddEdge(x << 1 | 1, y << 1 | 1); } cnt += p * 2; } if (Check()) printf("TAK\n"); else printf("NIE\n"); return 0;}
0 1
- BZOJ 3495 [2-SAT]
- bzoj 3495: PA2010 Riddle 2-sat
- [BZOJ]3495: PA2010 Riddle 2-SAT
- bzoj(1823)(2-SAT)
- [BZOJ 1823] 满汉全席 2-SAT
- [BZOJ]3495 Riddle 2-Sat 前缀和优化
- BZOJ 1823 JSOI 2010 满汉全席 2-SAT
- BZOJ 1997 HNOI 2010 Planar 2-SAT
- BZOJ 1997 Hnoi2010 Planar 2-sat
- BZOJ 1823 JSOI 2010 满汉全席 2-SAT
- BZOJ 1997 HNOI 2010 Planar 2-SAT
- bzoj 1823: [JSOI2010]满汉全席 2-SAT判定
- |BZOJ 1823|2-SAT|[JSOI2010]满汉全席
- bzoj 1823: [JSOI2010]满汉全席 (2-SAT)
- bzoj 1997: [Hnoi2010]Planar (2-SAT)
- BZOJ 1823 [JSOI2010]满汉全席 2-SAT
- BZOJ 1997: [Hnoi2010]Planar 2-sat
- bzoj 1997: [Hnoi2010]Planar 2-SAT+平面图
- 【前端笔试】关于cookie的设置,获取和移除
- 传奇sound文件调用说明
- 1043. Is It a Binary Search Tree (25)
- 深入理解Android指纹
- sublime
- BZOJ 3495 [2-SAT]
- android音频系统解析
- html表单
- 剑指offer-面试题 16:反转链表
- 02 快速上手OpenCV
- 数值计算:一重积分计算的C++实现
- MATLAB中已知圆心和半径画圆
- Codeforces Round #409 div2 D
- javascript基础编程应用表单验证