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 ;
}

最新文章

  1. 如何在Windows Server 2008 上添加RD (远程桌面)会话主机配置的远程桌面授权服务器
  2. javascript 数字进制转换
  3. 【转】【MySQL】mysql 通过bin-log恢复数据方法详解
  4. topcoder SRM 618 DIV2 LongWordsDiv2
  5. UVALive 7472
  6. 串口通信类,WPF
  7. 【Todo】【读书笔记】Java多线程编程指南-设计模式篇
  8. ubuntu 点点滴滴
  9. 【英文】Bingo口语笔记(18) - Cover系列
  10. cocos2d-x CCAction:动作(转)
  11. “Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象
  12. utf8格式源代码中的字符串,默认都会当作char来处理,除非用L&quot;&quot;符号来修饰
  13. [转]Mysql explain用法和性能分析
  14. Android 启动模式--任务(Task)--桟 的误区
  15. C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容
  16. Windows下phpstudy配置tp5的nginx时遇到的奇葩问题
  17. shiro源码篇 - shiro的filter,你值得拥有
  18. Codeforces 1079D Barcelonian Distance(计算几何)
  19. java笔记 -- GregorianCalendar和DateFormateSymbols 类方法
  20. 覃超:Facebook的项目开发流程和工程师的绩效管理机制

热门文章

  1. 重新认识vue之事件阻止冒泡
  2. platform_set_drvdata 和 platform_get_drvdata
  3. 《unix环境高级编程》学习笔记【原创】
  4. underscore.js中模板函数应用
  5. SDUT OJ 进制转换
  6. HDU1133 Buy the Ticket —— 卡特兰数
  7. linux /usr /var /etc 目录
  8. (QA-LSTM)自然语言处理:智能问答 IBM 保险QA QA-LSTM 实现笔记.md
  9. template &lt;typename T&gt;模板类定义
  10. bzoj 4289 PA2012 Tax——构图