题目链接

BZOJ3495

题解

每个城市都有选和不选两种情况,很容易考虑到2-sat

边的限制就很好设置了,主要是每个郡只有一个首都的限制

我们不可能两两之间连边,这样复杂度就爆炸了

于是乎就有了一个非常厉害的方法:

前缀后缀和

我们令\(1\)表示选,\(0\)表示不选,维护一个郡的前缀和、后缀和

一个点如果作为首都,那么它和它对应前缀后缀和的位置一定为\(1\),且之前的位置一定为\(0\)

然后再加上一些前缀后缀和固有的限制

就可以\(A\)啦

这样的建图太神了

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 6000005,maxv = 1000005,maxm = 15000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,K,N,pre[maxv][2],sa[maxv][2],p[maxv][2];
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],top,scci,cnt;
int h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
inline void build(int u,int v){
ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
}
void dfs(int u){
dfn[u] = low[u] = ++cnt;
st[++top] = u;
Redge(u){
if (!dfn[to = ed[k].to]){
dfs(to);
low[u] = min(low[u],low[to]);
}
else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
}
if (dfn[u] == low[u]){
scci++;
do{
Scc[st[top]] = scci;
}while (st[top--] != u);
}
}
int main(){
n = read(); m = read(); K = read();
REP(i,n) p[i][0] = ++N,p[i][1] = ++N;
int a,b;
while (m--){
a = read(); b = read();
build(p[a][0],p[b][1]);
build(p[b][0],p[a][1]);
}
int x,u;
while (K--){
x = read();
REP(i,x){
pre[i][0] = ++N,pre[i][1] = ++N;
sa[i][0] = ++N,sa[i][1] = ++N;
}
REP(i,x){
u = read();
build(p[u][1],pre[i][1]); build(pre[i][0],p[u][0]);
build(p[u][1],sa[i][1]); build(sa[i][0],p[u][0]);
if (i < x){
build(p[u][1],sa[i + 1][0]); build(sa[i + 1][1],p[u][0]);
build(pre[i][1],pre[i + 1][1]); build(pre[i + 1][0],pre[i][0]);
}
else build(pre[i][0],pre[i][1]);
if (i > 1){
build(p[u][1],pre[i - 1][0]); build(pre[i - 1][1],p[u][0]);
build(sa[i][1],sa[i - 1][1]); build(sa[i - 1][0],sa[i][0]);
}
else build(sa[i][0],sa[i][1]);
}
}
for (int i = 1; i <= N; i++) if (!dfn[i]) dfs(i);
for (int i = 1; i <= N; i += 2)
if (Scc[i] == Scc[i + 1]){
puts("NIE");
return 0;
}
puts("TAK");
return 0;
}

BZOJ3495 PA2010 Riddle 【2-sat】的更多相关文章

  1. BZOJ3495 : PA2010 Riddle

    2-SAT. 建立n个变量,其中第i个变量表示第i个城市是否是首都. 对于边(x,y),连边x->y',y->x'. 对于一个有y个城市的国家,新建2y个变量,分别表示前i个城市和后i个城 ...

  2. 【WebApi系列】浅谈HTTP

    [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi如何传递参数 [04]详解WebApi测试和PostMan [05]浅谈WebApi Core ...

  3. 【WebApi系列】浅谈HTTP在WebApi开发中的运用

    WebApi系列文章 [01]浅谈HTTP在WebApi开发中的运用 [02]聊聊WebApi体系结构 [03]详解WebApi参数的传递 [04]详解WebApi测试和PostMan [05]浅谈W ...

  4. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

    前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...

  5. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

  6. 【插头DP】BZOJ1187- [HNOI2007]神奇游乐园

    [题目大意] 在n*m的网格中选一条回路,使权值和最大. [思路] 和之前裸的插头DP差不多,只不过现在回路不需要经过所有的格子.所以有以下几个注意点(具体看注释): (1)left和up插头相等的时 ...

  7. zz【清华NLP】图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐

    [清华NLP]图神经网络GNN论文分门别类,16大应用200+篇论文最新推荐 图神经网络研究成为当前深度学习领域的热点.最近,清华大学NLP课题组Jie Zhou, Ganqu Cui, Zhengy ...

  8. 【故障解决】OGG-00446 错误解决

    [故障解决]OGG-00446 Could not find archived log for sequence 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读和 ...

  9. 【故障解决】ORA-06502错误解决

    [故障解决]ORA-06502: PL/SQL: numeric or value error: character string buffer too small 一.1  BLOG文档结构图   ...

随机推荐

  1. ORA-15032、ORA-15071错误处理

    遇到一下错误 ERROR at line 1: ORA-15032: not all alterations performed ORA-15071: ASM disk "NOCR_0002 ...

  2. python中的class正确用法

    class Dog: def __init__(self, name): self.name = name self.tricks = [] # creates a new empty list fo ...

  3. Ubuntu 14.04 登录 界面添加 root账号

    1打开终端输入:sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 2在弹出的编辑框里加入:greeter-show-manual- ...

  4. 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)

    题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...

  5. Python 发邮件例子

    Python 发邮件例子 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12:33 # @Autho ...

  6. solidity 十六进制字符串转十六进制bytes

    pragma solidity ^0.4.16; contract Metadata { // 十六进制字符串转换成bytes function hexStr2bytes(string data)re ...

  7. Python3 迭代器,生成器,装饰器

    1.迭代器 迭代器有两个基本方法,iter()和next(),next()完成后会引发StopIteration异常 a='abcdef' b=iter(a) #创建迭代器对象 print(type( ...

  8. 创建https证书

    第一个里程碑:创建https证书 创建文件认证目录 mkdir /application/nginx/key/ -p 在认证目录下创建认证文件 openssl req -new -x509 -node ...

  9. 《剑指Offer》题十一~题二十

    十一.旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如,数组{3, 4, 5, 1, 2}为{ ...

  10. ava中普通代码块,构造代码块,静态代码块区别及示例

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...