思路:

并查集维护每个开关的状态on[i]和off[i] 。
假设灯L由开关S1和S2控制。
如果开关是亮的,则S1和S2的状态相反;
如果开关是灭的,则S1和S2的状态相同。
当一个开关状态已知时,可以得知另一个开关的状态,合并。
如果on[i]和off[i]在同一个集合就无解。
时间复杂度:O((n+m)α(n))。
当然也可以二分图判定。

 #include<cstdio>
#define on(i) i
#define off(i) i+m
const int M=,N=;
class DisjointSet {
private:
int anc[M<<];
int Find(const int x) {
return (x==anc[x])?x:(anc[x]=Find(anc[x]));
}
public:
DisjointSet(const int m) {
for(int i=;i<=(m<<);i++) {
anc[i]=i;
}
}
void Union(const int x,const int y) {
anc[Find(x)]=Find(y);
}
bool isConnected(const int x,const int y) {
return Find(x)==Find(y);
}
};
int r[N];
int l[N][]={};
int main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {
scanf("%d",&r[i]);
}
for(int i=;i<=m;i++) {
int x;
scanf("%d",&x);
while(x--) {
int d;
scanf("%d",&d);
l[d][l[d][]?:]=i;
}
}
DisjointSet s(m);
for(int i=;i<=n;i++) {
if(!r[i]) {
s.Union(on(l[i][]),off(l[i][]));
s.Union(on(l[i][]),off(l[i][]));
}
else {
s.Union(on(l[i][]),on(l[i][]));
s.Union(off(l[i][]),off(l[i][]));
}
}
for(int i=;i<=m;i++) {
if(s.isConnected(on(i),off(i))) {
puts("NO");
return ;
}
}
puts("YES");
return ;
}

[CF776D]The Door Problem的更多相关文章

  1. CF776D The Door Problem[2-SAT]

    翻译 对于一扇门,如果是关的,那么他必须使用其中一个开关开开来,如果是开的,要么使用两个开关,要么啥都不做.这样,每扇门恰好对应两种状态,要选一个. 考虑用2-SAT模型解决.连边的话是对于一个机关, ...

  2. CF776D The Door Problem [2sat]

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

  3. 1199 Problem B: 大小关系

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

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

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

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

  6. Time Consume Problem

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

  7. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  8. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  9. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

随机推荐

  1. 内核IS_ERR宏解析 【转】

    转自:http://blog.chinaunix.net/uid-20196318-id-28769.html 最近在使用filp_open打开文件时遇到到一个问题,当打开一个并不存在的文件时,fil ...

  2. php学习之mysqli的面向对象

    // mySqlTool.php  封装好的工具类 <?php class SqlTool{ private $conn; private $host="localhost" ...

  3. BOvW简介

    原文地址:http://blog.csdn.net/ddreaming/article/details/52894379 BOW (bag of words) 模型简介 Bag of words模型最 ...

  4. Docker+STF在ubuntu下测试环境搭建(详细搭建步骤及踩坑记录)

    一.什么是OpenSTF? STF又称OpenSTF,它是一个手机设备管理平台,可以对手机进行远程管理.调试.远程手机桌面监控等操作.这个系统类似于目前很流行的云测服务比如Testin,虽然网页上提供 ...

  5. client模式下对应接口加入桥接出错

    client模式下,响应的接口wlan0 加入桥接时出现如下错误: root@root:~# brctl addif br-lan wlan0brctl: bridge br-lan: Operati ...

  6. Vue源码

    参考文章:http://hcysun.me/2017/03/03/Vue%E6%BA%90%E7%A0%81%E5%AD%A6%E4%B9%A0/?utm_source=qq&utm_medi ...

  7. CSS伪元素before、after妙用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. hdu 6125 状压dp+分组

    一道玄学题... 其实一开始想的是对的,优化一下就好了 首先我们会发现,乘积不能被完全平方数整除等价于所有因子的每个质因子个数和都至多为1 可是500以内的质数很多,全找出来会爆炸的 可我们会发现,如 ...

  9. Unicode转义序列

    声明: web前端学习笔记,欢迎大神指点.联系QQ:1522025433. Javascipt 定义了一种特殊序列,使用6位ASCII字符代表任意16Unicode内码.这些Unicode转义序列均以 ...

  10. oracle表分区的,分区操作,分区查询,子分区查询

    一.摘要 有关表分区的一些维护性操作: 注:分区根据具体情况选择. 表分区有以下优点: 1.数据查询:数据被存储到多个文件上,减少了I/O负载,查询速度提高. 2.数据修剪:保存历史数据非常的理想. ...