inputs: I found an interesting problem on https://codeforces.com/problemset/problem/776/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.


-----Input-----

First line of input contains two integers n and m (2 ≤ n ≤ 10^5, 2 ≤ m ≤ 10^5) — the number of rooms and the number of switches.

Next line contains n space-separated integers r_1, r_2, ..., r_{n} (0 ≤ r_{i} ≤ 1) which tell the status of room doors. The i-th room is locked if r_{i} = 0, otherwise it is unlocked.

The i-th of next m lines contains an integer x_{i} (0 ≤ x_{i} ≤ n) followed by x_{i} 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 1 to 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...

I tried it in Python, but could not do it. Can you solve it?
targets: import sys
import collections

n, m = list(map(int, input().split()))

r = tuple(map(int, input().split()))

controls = [tuple(map(int, input().split()))[1:] for i in range(m)]


class DSU:

    def __init__(self):
        self.parent = None
        self.has_zero = False
        self.has_one = False
        self.size = 1
        self.doors = []

    def get_root(self):
        if self.parent is None:
            return self
        self.parent = self.parent.get_root()
        return self.parent

    def unite(self, s):
        r1 = self.get_root()
        r2 = s.get_root()

        if r1 is r2:
            return r1

        if r1.size < r2.size:
            r1, r2 = r2, r1

        r2.parent = r1
        r1.size += r2.size
        r1.has_zero = r1.has_zero or r2.has_zero
        r1.has_one = r1.has_one or r2.has_one

        return r1

door_dsus = [[] for i in range(n)]
for doors in controls:
    n = DSU()
    for door in doors:
        n.doors.append(door - 1)

        door_dsus[door - 1].append(n)
        if r[door - 1]:
            n.has_one = True
        if not r[door - 1]:
            n.has_zero = True

for door, is_open in enumerate(r):
    n1, n2 = door_dsus[door]

    if is_open:
         n1.unite(n2)

G = {}
for door, is_open in enumerate(r):
    if is_open:
        continue

    n1, n2 = door_dsus[door]
    if n1.get_root() is n2.get_root():
        print("NO")
        return

    G.setdefault(n1.get_root(), set()).add(n2.get_root())
    G.setdefault(n2.get_root(), set()).add(n1.get_root())

color = {}

for v in list(G.keys()):
    if v in color:
        continue

    color[v] = False
    q = collections.deque([v])
    while q:
        v = q.popleft()
        c = color[v]
        for adj_v in G[v]:
            if adj_v in color:
                if color[adj_v] != (not c):
                    print("NO")
                    return
            else:
                color[adj_v] = not c
                q.append(adj_v)

print("YES")