ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D
Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.
You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.
You need to tell Sherlock, if there exists a way to unlock all doors at the same time.
First line of input contains two integers n and m (2 ≤ n ≤ 105, 2 ≤ m ≤ 105) — the number of rooms and the number of switches.
Next line contains n space-separated integers r1, r2, ..., rn (0 ≤ ri ≤ 1) which tell the status of room doors. The i-th room is locked if ri = 0, otherwise it is unlocked.
The i-th of next m lines contains an integer xi (0 ≤ xi ≤ n) followed by xi distinct integers separated by space, denoting the number of rooms controlled by the i-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1to n. It is guaranteed that each door is controlled by exactly two switches.
Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes.
3 3
1 0 1
2 1 3
2 1 2
2 2 3
NO
3 3
1 0 1
3 1 2 3
1 2
2 1 3
YES
3 3
1 0 1
3 1 2 3
2 1 2
1 3
NO
In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1 — unlocked).
After toggling switch 3, we get [0, 0, 0] that means all doors are locked.
Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.
It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked.
题意:一个门受到两个开关控制,问最后能不能把门全部打开(详情看输入格式和解释)
解法:
1 如果门是开的,那么控制的两个开关要么一起关,要么一起开
2 如果门是关的,那么控制的两个开关只需要开一个
3 不能全部开的条件,很幸运,经过分析发现 有一个控制关a门,也控制开a门,那么这门是打不开了(矛盾了)
4 我们把开关看做点,门看做线,没有+m看作是开门,+m看作关门,用并查集处理,找到一个开门和关门处于同一个集合的情况,矛盾不存在可行结果
#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const int maxn=1e5;
vector<int>Ve[*maxn];
int tree[*maxn];
int Find(int x)
{
if(x==tree[x])
return x;
return tree[x]=Find(tree[x]);
} void Merge(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
tree[fx]=fy;
}
int door[maxn];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=*m;i++){
tree[i]=i;
}
for(int i=;i<=n;i++){
scanf("%d",&door[i]);
}
for(int i=;i<=m;i++){
int num;
scanf("%d",&num);
for(int j=;j<=num;j++){
int x;
scanf("%d",&x);
Ve[x].push_back(i);
}
}
for(int i=;i<=n;i++){
int x=Ve[i][];
int y=Ve[i][];
if(door[i]){
Merge(x,y);
Merge(x+m,y+m);
}else{
Merge(x,y+m);
Merge(x+m,y);
}
}
int flag=;
for(int i=;i<=m;++i){
if(Find(i)==Find(i+m)){
flag=;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return ;
}
ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D的更多相关文章
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀
A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT
题目链接:http://codeforces.com/contest/776/problem/D D. The Door Problem time limit per test 2 seconds m ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...
- 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理. 然后跑强连通分量分解,保证a和非a不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C
Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B
Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...
随机推荐
- [noi2002]荒岛野人 拓展欧几里得
克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些山洞顺时针编号为1,2,…,M.岛上住着N个野人,一开始依次住在山洞C1,C2,…,CN中,以后每年,第i个野人会沿顺时针向前走Pi个洞住下来 ...
- 如何配置DSI时钟频率
[DESCRIPTION] 计算DSI数据速率的方式,以及如何配置时钟clk的方式 [KEYWORD] dsi.data rate.mipi clk [SOLUTION] 1.DSI vdo mode ...
- 数据结构之 线性表---单链表的操作B(先逆序+再删除重复元素)
数据结构上机测试2-2:单链表操作B Time Limit: 1000MS Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删 ...
- Appium——Error while obtaining UI hierarchy XML file:com.android.ddmlib.SyncException:
使用uiautomatorviewer查看页面元素时报这个错误,解决办法 cmd: adb root ok 解决
- Nginx的Location正则表达式
location的作用 location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作. location的语法 已=开头表示精确匹 ...
- 【转载】 C++中回车换行(\n\r)和换行(\r)的区别
原文:http://blog.csdn.net/xiaofei2010/article/details/8458605 windows下的点一下回车,效果是:回车换行,就是\r\n unix系统下的回 ...
- Java丨时间判断谁前谁后
直奔主题: String date_str1 = "2016-06-02 23:03:123"; String date_str2 = "2016-06-03 03:03 ...
- 使用cygwin注意事项二
使用cygwin时,一定要区分当前运行的是cygwin下的进程还是windows下的进程,如:使用vim, 假如cygwin下没安装vim, windows下安装了,那么你运行的就是windows下的 ...
- codevs 1046 旅行家的预算
传送门 1046 旅行家的预算 1999年NOIP全国联赛普及组NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold题解 题目描述 Des ...
- 洛谷 1082 同余方程——exgcd(水题)
题目:https://www.luogu.org/problemnew/show/P1082 大水题. #include<iostream> #include<cstdio> ...