Codeforces 776D The Door Problem
题目链接: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的更多相关文章
- 【codeforces 776D】The Door Problem
[题目链接]:http://codeforces.com/contest/776/problem/D [题意] 每个门严格由两个开关控制; 按一下开关,这个开关所控制的门都会改变状态; 问你能不能使所 ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- Codeforces 442B Andrey and Problem(贪婪)
题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...
- CodeForces 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- codeforces 803G Periodic RMQ Problem
codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...
- [Codeforces 986E] Prince's Problem
[题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...
- 【codeforces 527D】Clique Problem
[题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...
- 【codeforces 793C】Mice problem
[题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
随机推荐
- [转载]ASP.NET页面之间传递值的几种方式
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...
- ASCII字符代码表
- apache编译安装php后需要注意以下配置
安装后, 编辑apache配置文件 vi /usr/local/apache2/conf/httpd.conf 可以看到 LoadModule php7_module modules/libphp7. ...
- Redis可以用来做什么?(摘自http://www.lianpenglin.cc廉鹏林博客)
Redis 是互联网技术领域使用最为广泛的存储中间件,它是「Remote Dictionary Service」的首字母缩写,也就是「远程字典服务」.Redis 以其超高的性能.完美的文档.简洁易懂的 ...
- js DOM常见事件
js事件命名为on+动词 1.onclick事件,点击鼠标时触发,ondbclick双击事件 <h1 onclick="this.innerHTML='点击后文本'"> ...
- Spring Boot(十七):使用Spring Boot上传文件
Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- Docker 配置
1. 网络 使用redsocks 需要配置 iptables -t nat -A PREROUTING -p tcp -j REDSOCKS 还需要使能 route_localnet # settin ...
- 20145320《网络对抗》注入Shellcode并执行
20145320注入Shellcode并执行 准备一段Shellcode 首先先准备一段C语言代码:这段代码其实和我们的shell功能基本一样 为了之后能够看到反汇编的结果,这次采用的静态编译.正常返 ...
- Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树
题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...