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.

Input

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

Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes.

Examples
input
3 3
1 0 1
2 1 3
2 1 2
2 2 3
output
NO
input
3 3
1 0 1
3 1 2 3
1 2
2 1 3
output
YES
input
3 3
1 0 1
3 1 2 3
2 1 2
1 3
output
NO
Note

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

    前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...

  5. 【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不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...

  6. 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...

  7. 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 ...

  8. 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 ...

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...

随机推荐

  1. Dubbo与Zookeeper、SpringMVC整合与使用(干货-理论放一遍。。。还未完结!)

    Dubbo跟Zookeeper的简介分享两个不错的链接: Dubbo简介:http://shiyanjun.cn/archives/325.html Zookeeper简介:http://www.op ...

  2. Java中锁的内存语义

    我们都知道,Java中的锁可以让临界区互斥执行.锁是Java并发编程中最重要的同步机制,锁除了可以让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息.下面是锁的释放-获取的代码: C ...

  3. servlet理论学习

    servlet是和凭条无关的服务器端的组件,它运行在servlet容器中,servlet容器负责servlet和客户的通信以及调用servlet方法.servlet和客户的通信是采用“请求和响应的模式 ...

  4. Java(二)——开发环境搭建 安装JDK和配置环境变量

    1.安装JDK 下载地址  http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载 ...

  5. python之yield和Generator

    首先我们从一个小程序导入,各定一个list,找出其中的素数,我们会这样写 import math def is_Prims(number): if number == 2: return True / ...

  6. python 基础之第八天--字典相关

    zx #####################创建字典###################################### In [11]: dict([('name','bob'),('a ...

  7. a标签无法传递中文参数问题的解决

    a标签无法传递中文参数问题的解决. 可以通过form表单提交 隐藏域的方法解决. 前台jsp页面: <a class="vsb_buton" href="javas ...

  8. JavaScript DOM 编程艺术 ---> JavaScript语法

    二.  JavaScript语法目录 2.1 语法 javaScript代码要通过HTML/XHTML文档才能执行.可以有两种方式完成这一点,第一种是将JavaScript代码放到文档<head ...

  9. bzoj 2238 Mst——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2238 一条非树边可以对一条链的树边产生影响.注意是边,所以把边下放到点上,只要跳 top 时 ...

  10. web应用目录结构

    news web(应用的名字)||--静态资源和JSP文件都可以直接放在web应用的目录下,浏览器可以直接访问(html/jsp/css)|--WEB-INF 可以没有,但是最好有,一旦有,则结构需要 ...