翻译

对于一扇门,如果是关的,那么他必须使用其中一个开关开开来,如果是开的,要么使用两个开关,要么啥都不做。这样,每扇门恰好对应两种状态,要选一个。

考虑用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]的更多相关文章

  1. [CF776D]The Door Problem

    思路: 并查集维护每个开关的状态on[i]和off[i] .假设灯L由开关S1和S2控制.如果开关是亮的,则S1和S2的状态相反:如果开关是灭的,则S1和S2的状态相同.当一个开关状态已知时,可以得知 ...

  2. CF776D The Door Problem [2sat]

    考虑 \(\texttt{2-SAT}\) 首先每个门 \(i\) 都有一个初始状态 \(a_i\) 题目条件每个门只被两个开关控制,那么很显然的 \(\texttt{2-SAT}\) 用 \(b_{ ...

  3. 最新证明面临质疑:P/NP问题为什么这么难?

    转自:http://tech.sina.com.cn/d/2017-08-16/doc-ifyixias1432604.shtml 编译 | 张林峰(普林斯顿大学应用数学专业博士研究生) 责编 | 陈 ...

  4. LA 3211 飞机调度(2—SAT)

    https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...

  5. 《算法概论》第八章的一些课后题目 关于NP-Complete Problem

    8.3 STINGY SAT STINGY SAT is the following problem: given a set of clauses (each a disjunction of li ...

  6. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  7. 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 ...

  8. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  9. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

随机推荐

  1. 最新 唯品会java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.唯品会等10家互联网公司的校招Offer,因为某些自身原因最终选择了唯品会.6.7月主要是做系统复习.项目复盘.LeetCo ...

  2. 【SVN】总结:svn“Previous operation has not finished; run 'cleanup' if it was interrupted“

    svn执行clean up命令时报错“Previous operation has not finished; run 'cleanup' if it was interrupted”.无论你到那个父 ...

  3. Linux中 cmake-3.x 编译安装以及man page添加

    首先回顾一下 cmake-2.x 的编译安装. ================ cmake-2.x编译安装说明 ================编译安装的命令: ./bootstrap --pref ...

  4. elasticsearch 映射 dynamic用法

  5. Kubernetes---网络通讯模式笔记

    ⒈kubernetes网络通讯模式     Kubernetes的网络模型假定了所有Pod都在一个可以直接连通的扁平的网络空间中,这在GCE(Google Compute Engine)里面是现成的网 ...

  6. 超级简单的requests模块教程

    在web后台开发过程中,会遇到需要向第三方发送http请求的场景,python中的requests库可以很好的满足这一要求,这里简要记录一下requests模块的使用! 说明: 这里主要记录一下req ...

  7. PHP后台开发小经验

    js页面传参数 js的参数传输是关键,尤其是当一个页面的数据需要分步骤上传时. 同样的删除功能,不会操作批量删除时可以尝试单个删除,功能差不多,实现功能的方法也千千万,先做成它是第一位. 主页面很多条 ...

  8. Selenium工具爬取商品

    selenium是一个优秀的自动化测试工具,支持多种语言,具体介绍参考官方文档:https://www.seleniumhq.org/docs/. 下面我们使用selenium工具模拟用户点击商品详情 ...

  9. spark异常篇-关闭程序

    在运行 spark 程序时,出于某种原因,我想停止运行,狂按 ctrl+c 不一定起作用 以下两种情况是不好关闭的 1. cluster 运行模式 2. SparkStreaming 程序 本文旨在收 ...

  10. S02_CH09_UART串口中断实验

    S02_CH09_UART串口中断实验 本章的UART中断将在之前PL_PS中断和定时器中断上推导出来,因此本章有点难度,如果前两章还不是很熟悉的话,需要返回到前面两章把这两章的内容再次消化一下,再来 ...