题目链接

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. iOS - Foundation相关

    1.NSString         A.创建的方式:            stringWithFormat:格式化字符串  ,创建字符串对象在堆区域            @"jack& ...

  2. oracle的数据对象

    oracle的数据对象包括表.视图.约束.序列.索引.函数.存储过程.包和触发器等. 这里主要介绍视图.序列.索引.触发器.存储过程 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以 ...

  3. [JSON].typeOf( keyPath )

    语法:[JSON].typeOf( keyPath ) 返回:[String | Number | Boolean | Json | Array |  Function | 空字符] 说明:获取指定键 ...

  4. 孤荷凌寒自学python第七十七天开始写Python的第一个爬虫7

    孤荷凌寒自学python第七十七天开始写Python的第一个爬虫7 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 今天的学习仍然是在纯粹对docx模 ...

  5. AngularJS 初探

    AngularJS,诞生于2009年,由Misko Hevery等人创建,后为Google所收购.这是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为 ...

  6. springMVC第二章

    springMVC第二章 一.URL 映射 可以同时设置多个URL来访问某个控制器或方法.设置value属性: @RequestMapping(value= {"/grade",& ...

  7. 259 [LeetCode] 3Sum Smaller 三数之和较小值

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  8. tomcat 运行机制

    先不去关技术细节,对一个servlet容器,我觉得它首先要做以下事情:1:实现Servlet api规范.这是最基础的一个实现,servlet api大部分都是接口规范.如request.respon ...

  9. 【转】jQuery的deferred对象详解

    jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本. 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5.0版本开始引入的一个新功能----deferred对象. ...

  10. 自测之Lesson4:gdb

    题目:列出gdb过程中常用的命令. 常用命令: 命令 作用 使用示例1 使用示例2 list 列出代码 list 行号 list 函数名 break 设置断点 break 行号 b 行号 run 运行 ...