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. sqlserver锁表查看

    sp_lock--查询哪个进程锁表了,spid:进程ID,ObjId:对象ID EXEC sp_executesql N'KILL [spid]'--杀进程 select object_name([O ...

  2. SystemV和BSD的区别

    目前,Unix操作系统不管其内核如何,其操作风格上主要分为SystemV(目前一般采用其第4个版本SVR4)和BSD两种.其代表操作系统本别是Solaris和FreeBSD.当然,在SunOS4(So ...

  3. hdu-5000 Clone(dp)

    题目链接: Clone Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Pro ...

  4. adb pull / push

    刚才搞了半天想pull,就是pull不成,如图: 看出哪里有问题了吗? 问题就是我不该在shell里面运行adb pull! 正确的做法: 在任意一处打开命令行比如图中的桌面, adb pull /s ...

  5. 安装asterisk以及asterisk-gui

           asterisk的安装在ubuntu上自我感觉还是很方便的,虽然也会遇到一些小的问题.下面是本人遇到的   一些问题和解决方法.     1>在ubuntu10.04上安装aste ...

  6. BZOJ_2730_ [HNOI2012]矿场搭建_点双联通分量

    BZOJ_2730_ [HNOI2012]矿场搭建_点双联通分量 Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路 ...

  7. codevs 1497取余运算

    1497 取余运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamon   题目描述 Description 输入b,p,k的值,编程计算bp mod k的值. ...

  8. C++模板之函数模板实例化和具体化

    模板声明 template<typename/class T>,  typename比class最近后添加到C++标准. 常规模板,具体化模板,非模板函数的优先调用顺序. 非模板函数(普通 ...

  9. hibernate 中的拦截器EmptyInterceptor接口功能

    Interceptor接口提供了从会话(session)回调(callback)应用程序(application)的机制, 这种回调机制可以允许应用程序在持久化对象被保存.更新.删除或是加载之前,检查 ...

  10. POJ2406(next原理理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 40448   Accepted: 16828 D ...