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轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
随机推荐
- Redhat 简单本地yum 配置
Redhat 简单本地yum 配置 一.将redhat 系统的镜像挂载到系统上 Vmware Workstion 环境下: [虚拟机设置]--[硬件]--[CD/DVD]--[使用ISO映像文件]-- ...
- Autel MaxiSys Elite Diagnostic Tool Common problem solving methods
1. updating MaxiFlash Elite to firmware 3.21? My maxisys communicate with the MaxiFlash J2534 but Ma ...
- ASP.NET定时调用WebService 运行后台代码
效果: 通过在网站的Global.asax的Application_Start方法中 加入定时器 定时调用WebService 该WebService的一个方法 负责在后台 向数据库的某个表加入数据 ...
- (2018干货系列八)最新VR学习路线整合
怎么学VR 即虚拟现实技术,是一种可以创建和体验虚拟世界的计算机仿真系统,它利用计算机生成一种模拟环境,是一种多源信息融合的.交互式的三维动态视景和实体行为的系统仿真使用户沉浸到该环境中.VR/AR/ ...
- GoldenGate实时投递数据到大数据平台(1)-MongoDB
mongodb安装 安装 linux下可使用apt-get install mongodb-server 或 yum install mongodb-server 进行安装. 也可以在windows上 ...
- QT多线程简单例子
在Qt中实现多线程,除了使用全局变量.还可以使用信号/槽机制. 以下例子使用信号/槽机制. 功能: 在主线程A界面上点击按钮,然后对应开起一个线程B.线程B往线程A发送一个字符串,线程A打印出来. 1 ...
- Eloquent JavaScript #06# class
索引 Notes this Prototype 类 class符号 覆盖派生属性 Maps Symbols iterator接口 Getters, setters, and statics 继承 in ...
- Centos环境自写脚本查看使用php或nginx占用内存
在CentOs6.4下,用root权限测试. # cd ~ //进入home目录 # vim .bashrc //编辑文件,把下面代码放入地址 mem () { top -n1 -b | head - ...
- 【题解】 Luogu CF375D Tree and Queries
原题传送门 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 我博客中对莫队的详细介绍 莫队好题 我一上来想写线段树,随后觉得不好写并弃坑 我们可以看见没有修改操作,钦定莫队 但这是在树上,所以不能 ...
- 查看Linux服务器是否是虚拟机
dmidecode -s system-product-name 物理机: [root@swnode1]# dmidecode -s system-product-name I840-GS 虚拟机: ...