[CF776D]The Door Problem
思路:
并查集维护每个开关的状态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的更多相关文章
- CF776D The Door Problem[2-SAT]
翻译 对于一扇门,如果是关的,那么他必须使用其中一个开关开开来,如果是开的,要么使用两个开关,要么啥都不做.这样,每扇门恰好对应两种状态,要选一个. 考虑用2-SAT模型解决.连边的话是对于一个机关, ...
- CF776D The Door Problem [2sat]
考虑 \(\texttt{2-SAT}\) 首先每个门 \(i\) 都有一个初始状态 \(a_i\) 题目条件每个门只被两个开关控制,那么很显然的 \(\texttt{2-SAT}\) 用 \(b_{ ...
- 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 ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
随机推荐
- 用nodejs做一下发送邮件例子
var nodemailer = require("nodemailer"); var transport = nodemailer.createTransport("S ...
- Redis简介+常用命令
Redis=REmote DIctionary Server Redis是一个使用C语言编写的开源数据库,是高性能的key-value数据库,是内存数据库,支持数据持久化. Redis常用数据类型: ...
- 信息检索(IR)的评价指标介绍 - 准确率、召回率、F1、mAP、ROC、AUC
原文地址:http://blog.csdn.net/pkueecser/article/details/8229166 在信息检索.分类体系中,有一系列的指标,搞清楚这些指标对于评价检索和分类性能非常 ...
- linux系统下安装tomcat及配置
一.下载TOMCAT压缩包 apache-tomcat-6.0.44-client.zip 或 apache-tomcat-7.0.69.tar.gz 点击进入官网:http://tomcat.apa ...
- scn 时间
Scn转换成时间: select to_char(scn_to_timestamp(3998591352171),'YYYY-MM-DD HH24:MI:SS') from dual: 时间转换成sc ...
- Expm 1_2 实现快速排序的算法,并尝试采用不同的方法实现线性的划分过程.
package org.xiu68.exp.exp1; public class Exp1_2 { //实现快速排序算法,采用不同的方法实现线性划分的过程 public static void mai ...
- RHEL7 配置iSCSI模拟环境
在之前文章<multipath多路径实验01-构建iSCSI模拟环境>中,已经介绍了如何构建iSCSI模拟环境(RHEL6),但在RHEL7中已经不适用,本文记录下新的配置方法. 环境:R ...
- mpVue小程序全栈开发
1.微信小程序,mpVue和wepy的对比 2. 3.es6中关于数组的一些方法 <script> let arr = [,,,] // 遍历 arr.forEach(v => { ...
- 100以内与7有关的数(for和if)
- webpack-clean-webpack-plugin
在webpack中打包生成的文件会覆盖之前的文件,不过生成文件的时候文件名加了hash之后会每次都生成不一样的文件,这就会很麻烦,不但会生成很多冗余的文件,还很难搞清楚到底是哪个文件,这就需要引入该插 ...