CF776D.The Door Problem

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Moriarty has trapped nn people in nn 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 mm 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 11 , which was connected to room 11 , 22 and 33 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 nn and mm ( 2<=n<=1052<=n<=10^{5} , 2<=m<=1052<=m<=10^{5} ) — the number of rooms and the number of switches.

Next line contains nn space-separated integers r1,r2,...,rnr_{1},r_{2},...,r_{n} ( 0<=ri<=10<=r_{i}<=1 ) which tell the status of room doors. The ii -th room is locked if ri=0r_{i}=0 , otherwise it is unlocked.

The ii -th of next mm lines contains an integer xix_{i} ( 0<=xi<=n0<=x_{i}<=n ) followed by xix_{i} distinct integers separated by space, denoting the number of rooms controlled by the ii -th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 11 to nn . 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.

输入输出样例

  • 输入#1

    3 3
    1 0 1
    2 1 3
    2 1 2
    2 2 3
    

    输出#1

    NO
  • 输入#2

    3 3
    1 0 1
    3 1 2 3
    1 2
    2 1 3
    

    输出#2

    YES
  • 输入#3

    3 3
    1 0 1
    3 1 2 3
    2 1 2
    1 3
    

    输出#3

    NO

说明/提示

In the second example input, the initial statuses of the doors are [1,0,1][1,0,1] ( 00 means locked, 11 — unlocked).

After toggling switch 33 , we get [0,0,0][0,0,0] that means all doors are locked.

Then, after toggling switch 11 , we get [1,1,1][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.

首页