题目链接:http://codeforces.com/contest/776/problem/D


把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙。

我们知道一扇门一定对应了两把钥匙。

设一扇门对应的要是分别为$u,v$,${link(x,y)}$表示点$x$向点$y$连边。

如果这扇门要操作一次,那就是两把当中选一把:${link(u,v+m),link(v,u+m)}$。这表示的是如果我选了拿第$u$把钥匙就不能拿第$v$把钥匙,如果我选了拿第$v$把钥匙就不能拿第$u$把钥匙

如果这扇门不要操作,那就是两把当中选两把或者都不选:${link(u+m,v+m),link(v,u)}$。这表示的是如果我选了拿第$u$把钥匙就一定要拿第$v$把钥匙,如果我不拿第$v$把钥匙就一定不能拿第$u$把钥匙

这个是无向边啊,并查集维护一下关系即可。

对于${1...m}$中的每一把钥匙,如果${x,x+m}$属于一个连通块就无解了,因为包含关系成环。


 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 2001000
#define llg int
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,fa[maxn],k,c[maxn]; llg a[maxn][]; llg find(llg x) {if (fa[x]!=x) fa[x]=find(fa[x]); return fa[x];} void link(llg x,llg y)
{
llg f1=find(x),f2=find(y);
if (f2!=f1) fa[f2]=f1;
} int main()
{
yyj("D");
cin>>n>>m;
for (llg i=;i<=n;i++) scanf("%d",&c[i]);
for (llg i=;i<=m*;i++) fa[i]=i;
for (llg j=;j<=m;j++)
{
cin>>k;
llg x;
for (llg i=;i<=k;i++)
{
scanf("%d",&x);
a[x][++a[x][]]=j;
}
}
for (llg i=;i<=n;i++)
{
llg u=a[i][],v=a[i][];
if (!c[i]) link(u,v+m),link(u+m,v);else link(u,v),link(u+m,v+m);
}
for (llg i=;i<=m;i++) if (find(i)==find(i+m)) {cout<<"NO"; return ;}
cout<<"YES";
return ;
}

Codeforces 776D The Door Problem的更多相关文章

  1. 【codeforces 776D】The Door Problem

    [题目链接]:http://codeforces.com/contest/776/problem/D [题意] 每个门严格由两个开关控制; 按一下开关,这个开关所控制的门都会改变状态; 问你能不能使所 ...

  2. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  3. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  4. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  5. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  6. codeforces 803G Periodic RMQ Problem

    codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...

  7. [Codeforces 986E] Prince's Problem

    [题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...

  8. 【codeforces 527D】Clique Problem

    [题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...

  9. 【codeforces 793C】Mice problem

    [题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...

随机推荐

  1. hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)

    描述 Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral tria ...

  2. redis 五大数据类型以及操作

    一.redis的两种链接方式 1.简单连接 import redis conn = redis.Redis(host='10.0.0.200',port=6379) conn.set('k1','年后 ...

  3. MyEclipse/Eclipse快捷键总结

    MyEclipse/Eclipse快捷键 查找某个方法被谁调用:选中方法名,ctrl+shift+g 通过文件名称查找类或文件:ctrl+shift+r(Open Resource)

  4. 计算概论(A)/基础编程练习2(8题)/6:数组逆序重放

    #include<stdio.h> int main() { // 输入n个整数 ; scanf("%d", &n); // 循环读入元素 while(scan ...

  5. 利用apache伪静态技术防止盗链

    (在我们制作网站的过程中,可能会遇到这样的问题,就是其他的网站直接盗用了我们网站的图片或css或js,这样可能会大大增加我们自己网站的负载. 所以,我们应该考虑一下怎样防止这样的事情发生.) 下面我们 ...

  6. linux 安装 ImageMagick 和 imagick 扩展

    使用命令安装 1.依次运行以下命令 yum install ImageMagick yum install ImageMagick-devel yum install php-pear 安装php-p ...

  7. Spring学习笔记2:Spring HelloWorld

    1:IntelliJ新建Maven工程 2:pom文件加入Spring依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  8. 讨论下python中全局变量的使用

    首先看一段代码: A = 0 B = [0] def fun1(A, B): A += 1 B[0] += 1 fun1(A, B) print 'after fun1 %d %s' % (A,B) ...

  9. http请求记录

    Request Headers 请求头 Content-Type 默认值: "application/x-www-form-urlencoded".发送信息至服务器时内容编码类型 ...

  10. ODAC(V9.5.15) 学习笔记(三)TOraSession(2)

    2. 事务相关 名称 类型 说明 AutoCommit Boolean 是否自动提交事务 注意:只有当TOraSession和TOraQuery的AutoCommit都为True时才对每个数据库操作自 ...