翻译

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

考虑用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学习笔记-函数

    Java也有着函数的概念,不过在OOP中,函数用作方法称呼 函数的定义 函数就是定义在类中的具有特定功能的一段独立小程序 函数也称为方法 函数的格式 修饰符返回值类型函数名(参数类型形式参数1,参数类 ...

  2. Vue Cli3.0 使用jquery

    参考链接:https://blog.csdn.net/ai520587/article/details/84098601

  3. Java集合框架——Map接口

    第三阶段 JAVA常见对象的学习 集合框架--Map集合 在实际需求中,我们常常会遇到这样的问题,在诸多的数据中,通过其编号来寻找某一些信息,从而进行查看或者修改,例如通过学号查询学生信息.今天我们所 ...

  4. 使用power designer,PL/SQL,cmd建立oracle数据库

    这一系列操作需要powerDesigner,PL/SQL工具 1.首先使用powerDesigner建立概念模型 2.概念模型界面例子 3.其中建立概念模型操作图标详解 4.建立物理模型 5.生成数据 ...

  5. css 颜色自动变化 炫彩

    .layui-icon-login-qq:hover{ color:rgb(0, 156, 255); transition: 0.5s; animation:change 10s linear 0s ...

  6. RabbitMQ快速开始

    ①:安装rabbitmq所需要的依赖包 yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make g ...

  7. matplotlib 画图中的basemap安装问题

    在我用matplotlib画图是会用到,basemap这个库,但是直接安装却会出现问题: 使用这条安装命令就能够安装功能了: conda install -c conda-forge basemap= ...

  8. 【leetcode】287. 寻找重复数

    题目链接:传送门 题目描述: 给定一个数组 nums 包含 n + 1 个整数,每个整数在 1 到 n 之间,包括 1 和 n.现在假设数组中存在一个重复的数字,找到该重复的数字. 注意 不能修改数组 ...

  9. shell习题第17题:检测磁盘

    [题目要求] 写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时候,发邮件提醒 [核心要点] df d ...

  10. Java 关于String Pool

    下面的文章讲得挺清楚: https://www.baeldung.com/java-string-pool 再加一个关于虚拟机的,因为上面的文章提到了JVM: https://abhirockzz.w ...