Luogu 3825 [NOI2017]游戏
Luogu的spj现在挂了,要去其他OJ提交。
2-SAT
发现如果不考虑$x$的情况,这就成为一个2-SAT的裸题了,我们有$O(n + m)$的方法可以解决它。
那加上$x$的情况怎么弄……岂不是变成一个3-SAT。
滑稽吧,3-SAT已经被证明是一个完全NPC问题了……
再观察一下数据范围发现为$x$的点最多只有$8$个,那么我们思考一下(看一下题解)就会发现$x$的点取$a$或者$b$的情况其实就可以遍历到所有可行解了,所以直接取枚举这个$2^{d}$,然后$O(n + m)$地去检验它,时间复杂度$O(2^{d}(n + m))$。
连边方法(假设当前的条件是$x, c1, y, c2$):
1、如果第$x$场不能使用$x$,那么直接$continue$,这个条件显然没有影响。
2、如果第$x$场能使用$x$,第$y$场不能使用$y$,那么直接把$(x, true)$连向$(x, false)$,代表如果选了$(x, true)$就无解。
3、如果第$x$场可以使用$x$,第$y$场也可以使用$y$,那么按照套路连成一个对偶图,把$(x, true)$向$(y, true)$连边,同时把$(y, false)$向$(x, false)$连边。
关于$(x, true)$和$(x, false)$的记法,可以自己yy一下,要把$(x, true)$记为$x$, $(x, false)$记为$x + n$, 最后输出的时候对应回来就好。
Code:
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; const int N = 2e5 + ; int n, m, K, pos[], tot, head[N];
int dfsc, dfn[N], low[N], top, sta[N], scc, bel[N];
char str[N];
bool vis[N]; struct Eedge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} struct Restrain {
char c1, c2;
int x, y; inline void readIn() {
c1 = c2 = ;
read(x); for(c1 = getchar(); c1 != 'A' && c1 != 'B' && c1 != 'C'; c1 = getchar());
read(y); for(c2 = getchar(); c2 != 'A' && c2 != 'B' && c2 != 'C'; c2 = getchar());
} } a[N]; inline int id(int now, char c) {
if(str[now] == 'a') return c == 'C' ? now : now + n;
if(str[now] == 'b') return c == 'A' ? now : now + n;
if(str[now] == 'c') return c == 'B' ? now : now + n;
return ;
} inline int opp(int nowId) {
return nowId > n ? nowId - n : nowId + n;
} inline int min(int x, int y) {
return x > y ? y : x;
} void tarjan(int x) {
dfn[x] = low[x] = ++dfsc;
vis[x] = , sta[++top] = x;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if(vis[y]) low[x] = min(low[x], dfn[y]);
} if(low[x] == dfn[x]) {
++scc;
for(; sta[top + ] != x; --top) {
vis[sta[top]] = ;
bel[sta[top]] = scc;
}
}
} inline bool solve() {
dfsc = tot = top = scc = ;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(bel, , sizeof(bel));
memset(head, , sizeof(head)); for(int i = ; i <= m; i++) {
if(a[i].c1 + == str[a[i].x]) continue;
if(a[i].c1 == a[i].c2 && a[i].x == a[i].y) continue;
int p1 = id(a[i].x, a[i].c1), p2 = opp(p1);
int p3 = id(a[i].y, a[i].c2), p4 = opp(p3);
if(a[i].c2 + == str[a[i].y]) {
add(p1, p2);
continue;
}
add(p1, p3), add(p4, p2);
} for(int i = ; i <= * n; i++)
if(!dfn[i]) tarjan(i); for(int i = ; i <= n; i++)
if(bel[i] == bel[i + n]) return ; return ;
} inline void print() {
for(int i = ; i <= n; i++) {
if(bel[i] < bel[i + n]) {
if(str[i] == 'a') putchar('C');
if(str[i] == 'b') putchar('A');
if(str[i] == 'c') putchar('B');
} else {
if(str[i] == 'a') putchar('B');
if(str[i] == 'b') putchar('C');
if(str[i] == 'c') putchar('A');
}
}
exit();
} int main() {
read(n), read(K); scanf("%s", str + );
K = ;
for(int i = ; i <= n; i++)
if(str[i] == 'x') pos[++K] = i; /* for(int i = 1; i <= K; i++)
printf("%d ", pos[i]);
printf("\n"); */ read(m);
for(int i = ; i <= m; i++) a[i].readIn(); /* for(int i = 1; i <= m; i++)
printf("%d %c %d %c\n", a[i].x, a[i].c1, a[i].y, a[i].c2); */ for(int S = ; S < ( << K); S++) {
for(int i = ; i < K; i++)
if((S >> i) & ) str[pos[i + ]] = 'a';
else str[pos[i + ]] = 'b'; bool flag = solve();
if(flag) print();
} puts("-1");
return ;
}
Luogu 3825 [NOI2017]游戏的更多相关文章
- [Luogu P3825] [NOI2017] 游戏 (2-SAT)
[Luogu P3825] [NOI2017] 游戏 (2-SAT) 题面 题面较长,略 分析 看到这些约束,应该想到这是类似2-SAT的问题.但是x地图很麻烦,因为k-SAT问题在k>2的时候 ...
- 洛谷3825 [NOI2017]游戏 2-sat
原文链接http://www.cnblogs.com/zhouzhendong/p/8146041.html 题目传送门 - 洛谷3825 题解 我们考虑到地图中x的个数很少,最多只有8个. 所以我们 ...
- Luogu P3825 [NOI2017]游戏
这道题看上去NPC啊,超级不可做的样子. 我们先分析一下简单的情形:没有\(x\)地图 此时每个地图由于限制掉一种汽车,那么显然只会有两种选择. 再考虑到限制的情况,那么大致做法就很显然了--2-SA ...
- P3825 [NOI2017]游戏
题目 P3825 [NOI2017]游戏 做法 \(x\)地图外的地图好做,模型:\((x,y)\)必须同时选\(x \rightarrow y,y^\prime \rightarrow x^\pri ...
- 【BZOJ4945】[Noi2017]游戏 2-SAT
[BZOJ4945][Noi2017]游戏 题目描述 题解:2-SAT学艺不精啊! 这题一打眼看上去是个3-SAT?哎?3-SAT不是NPC吗?哎?这题x怎么只有8个?暴力走起! 因为x要么不是A要么 ...
- [luogu]P1070 道路游戏[DP]
[luogu]P1070 道路游戏 题目描述小新正在玩一个简单的电脑游戏.游戏中有一条环形马路,马路上有 n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针 ...
- BZOJ4945 & 洛谷3825 & UOJ317:[NOI2017]游戏——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4945 https://www.luogu.org/problemnew/show/P3825 ht ...
- bzoj3825 NOI2017 游戏
题目背景 狂野飙车是小 L 最喜欢的游戏.与其他业余玩家不同的是,小 L 在玩游戏之余,还精于研究游戏的设计,因此他有着与众不同的游戏策略. 题目描述 小 L 计划进行nn 场游戏,每场游戏使用一张地 ...
- [NOI2017]游戏(2-SAT)
这是约半年前写的题解了,就搬过来吧 感觉这是NOI2017最水的一题(当然我还是不会2333),因为是一道裸的2-SAT.我就是看着这道题学的2-SAT 算法一:暴力枚举.对于abc二进制枚举,对于x ...
随机推荐
- Yii2学习笔记---内附GridView配置总结
1./vendor/yiisoft/yii2/web/UrlManager.php 方法createUrl 修改url参数转码2.config/web.php 配置文件Yii::$app(应用主体)的 ...
- 【leetcode刷题笔记】Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- mysql高可用研究(二) 主从+MHA+Atlas
关于Atlas的详细介绍请访问:https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md 为什么要使用Atlas?应用程序直连数据库不好吗? ...
- 常见http返回状态码
200:表示从客户端发来的请求在服务器端被正常处理了. 302:临时重定向,该状态码表示请求的资源已经被分配了新的URI,希望用户本次能够通过新的UIRI访问. 304:未修改,服务端资源未改变,可直 ...
- POJ 1611并查集
我发现以后写题要更细心,专心! #include<iostream>#include<algorithm>#include<stdio.h>#include< ...
- Vue 5小时学习小教程
Vue Vue Vue 起步 指令 v-bind v-if v-for v-on v-model v-bind和v-on缩写 搭建Vue开发环境 vue项目结构 Vue开始 数据绑定, 绑定属性 循环 ...
- mysql 触发器(trigger) 总结
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...
- 分享知识-快乐自己:Java常用API总结
1):java.io.BufferedReader类(用于从文件中读入一段字符:所属套件:java.io) 1. 构造函数BufferedReader(java.io.FileReader FileR ...
- Tomcat翻译--Tomcat Web Application Deployment(Tomcat中部署web应用)
原文:http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html Introduction(介绍) Deployment is the te ...
- Python-获取前一条用例的执行结果
import unittest,HTMLTestRunner from nose_parameterized import parameterized def login(username,passw ...