CF776D The Door Problem[2-SAT]
对于一扇门,如果是关的,那么他必须使用其中一个开关开开来,如果是开的,要么使用两个开关,要么啥都不做。这样,每扇门恰好对应两种状态,要选一个。
考虑用2-SAT模型解决。连边的话是对于一个机关,所有他控制的门都应该一起选(具体地说,对于一扇关闭的门,这个机关是他的第几个机关,就是哪个状态,如果是开着的,必须对应使用开关两次的状态),所以这些状态点互相连双向边。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define mst(x) memset(x,0,sizeof x)
#define dbg(x) cerr << #x << " = " << x <<endl
#define dbg2(x,y) cerr<< #x <<" = "<< x <<" "<< #y <<" = "<< y <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=2e5+;
struct thxorz{
int head[N],to[N<<],nxt[N<<],tot;
inline void link(int x,int y){
to[++tot]=y,nxt[tot]=head[x],head[x]=tot;
to[++tot]=x,nxt[tot]=head[y],head[y]=tot;
}
}G;
int n,m;
int sta[N],vis[N],lock[N];
#define y G.to[j]
int dfn[N],low[N],stk[N],instk[N],Top,tim,scc,bel[N];
void tarjan(int x){
dfn[x]=low[x]=++tim,stk[++Top]=x,instk[x]=;
for(register int j=G.head[x];j;j=G.nxt[j]){
if(!dfn[y])tarjan(y),MIN(low[x],low[y]);
else if(instk[y])MIN(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
int tmp;++scc;//dbg(scc);
do instk[tmp=stk[Top--]]=,bel[tmp]=scc;while(tmp^x);
}
}
#undef y
int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
read(n),read(m);
for(register int i=;i<=n;++i)read(sta[i]);
for(register int i=,k;i<=m;++i){
read(k);
for(register int j=,x,las=;j<=k;++j,las=x){
lock[j]=read(x);
if(las){
if(sta[x])sta[las]?(G.link(x,las),G.link(x+n,las+n)):(G.link(x,las+vis[las]*n),G.link(x+n,las+(-vis[las])*n));
else sta[las]?(G.link(x+vis[x]*n,las),G.link(x+(-vis[x])*n,las+n)):(G.link(x+vis[x]*n,las+vis[las]*n),G.link(x+(-vis[x])*n,las+(-vis[las])*n));
}
}
for(register int j=;j<=k;++j)++vis[lock[j]];
}
for(register int i=;i<=n<<;++i)if(!dfn[i])tarjan(i);
for(register int i=;i<=n;++i)if(bel[i]==bel[i+n]){puts("NO");return ;}
puts("YES");return ;
}
注意要连反向边。。我忘连了所以挂了一次。。
总结:主要在于转化模型。。看到2数字要敏感。。把每种个体转化为对应的01状态。。
CF776D The Door Problem[2-SAT]的更多相关文章
- [CF776D]The Door Problem
思路: 并查集维护每个开关的状态on[i]和off[i] .假设灯L由开关S1和S2控制.如果开关是亮的,则S1和S2的状态相反:如果开关是灭的,则S1和S2的状态相同.当一个开关状态已知时,可以得知 ...
- CF776D The Door Problem [2sat]
考虑 \(\texttt{2-SAT}\) 首先每个门 \(i\) 都有一个初始状态 \(a_i\) 题目条件每个门只被两个开关控制,那么很显然的 \(\texttt{2-SAT}\) 用 \(b_{ ...
- 最新证明面临质疑:P/NP问题为什么这么难?
转自:http://tech.sina.com.cn/d/2017-08-16/doc-ifyixias1432604.shtml 编译 | 张林峰(普林斯顿大学应用数学专业博士研究生) 责编 | 陈 ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- 《算法概论》第八章的一些课后题目 关于NP-Complete Problem
8.3 STINGY SAT STINGY SAT is the following problem: given a set of clauses (each a disjunction of li ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
随机推荐
- 字符编码笔记:ASCII,Unicode 和 UTF-8(理解)
1.ASCII 码 美国制定的字符编码规则,对英语字符与二进制位之间的关系做了统一规定. 占一个字节,8 位,最多可表示 2^8 = 256 种状态(字符) 实际共有 128 个字符,只占用一个字节的 ...
- zTree入门使用
简单入门使用,熟悉其功能,没有与异步调用后台数据,用的是本地设置的数据. zTree的API:http://www.treejs.cn/v3/api.php 源码:https://github.com ...
- bootstrap-table的一些配置参数例子
$('#reportTable').bootstrapTable({ method: 'post', url: '/qStock/AjaxPage', dataType: "json&quo ...
- [转帖]XCopy复制文件夹命令及参数详解以及xcopy拷贝目录并排除特定文件
XCopy复制文件夹命令及参数详解以及xcopy拷贝目录并排除特定文件 https://www.cnblogs.com/smartsmile/p/7665979.html xcopy dirA dir ...
- 正式发布! .NET开发控件集ComponentOne 新版本加入Blazor UI
近期,由葡萄城推出的ComponentOne .NET开发控件集正式发布最新版本! ComponentOne 是一套专注于企业 .NET开发.支持 .NET Core 平台,并完美集成于 Visual ...
- 网络流Dinic--模板
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupp ...
- Python3 + selenium + Chrome浏览器(webdriver.Chrome()报错)
Python3 + selenium + Chrome浏览器 Error: selenium.common.exceptions.WebDriverException: Message: 'chrom ...
- WPF入门(1)——DataContext
在WPF中,应用程序有两层:UI层和Data层.这里新建一个项目说明哪些是UI层,哪些是数据层. UI层很明显,就是用户看到的界面.但是数据层并不是下图所示: 上图中是UI层view的后台代码.当然, ...
- CentOS7安装配置redis5集群
一.服务器准备 本文准备了3台服务器 , 分别是 172.18.0.231 172.18.0.232 172.18.0.233 每台运行2个redis实例, 端口分别为7000 7001 ,即总共6个 ...
- MySQL create table语法中的key与index的区别
在create table的语句中,key和index混淆在一起,官方手册中的解释是这样: KEY is normally a synonym for INDEX. The key attribute ...