Problem:


Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

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

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8



Sample input:
2 2
2 1 2
1 2
0 1


Sample output:
1

Submission status: Accepted

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


def calc_bitcount(switch_pattern, bit):
    x = switch_pattern & bit
    ret = 0
    while x:
        if x & 1:
            ret += 1
        x >>= 1
    return ret


def main():
    n, m = intin()
    kslistlist = []
    for i in range(m):
        kslistlist.append(intina())
    plist = intina()

    light_to_bit = defaultdict(int)
    for light, kslist in enumerate(kslistlist):
        for switch in kslist[1:]:
            light_to_bit[light] |= (1<<(switch - 1))

    ans = 0

    for switch_pattern in range(2**n):
        for light, bit in light_to_bit.items():
            bitcount = calc_bitcount(switch_pattern, bit)
            if bitcount & 1 != plist[light]:
                break
        else:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())

ks = [list(map(int, input().split())) for _ in range(M)]

p = list(map(int, input().split()))


def D2NC(D: int, N: int, link=False) -> list:
    """
    10進数表記の数字をN進数表記に変換する。
    (関数名は"Decimal to N_ary Converter"の略。)

    Parameters
    ----------
    D : 10進数表記で与えられた数字。
    N : 変換したい底。
    link : Trueにすると結果を文字列結合して返す。

    Returns
    -------
    res : 変換結果。デフォルトではリスト化して返す。
    """
    alp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    res = []
    while D > 0:
        if D % N > 9:
            tmp = alp[D % N - 10]
            res.insert(0, str(tmp))
        else:
            res.insert(0, str(D % N))
        D //= N
    if not res:
        res.append('0')
    if link:
        res = ''.join(res)
    return res


digit = N

ans = 0
for i in range(2**N):
    switch = D2NC(i, 2, True).zfill(digit)
    idx = 0
    flg = 1
    for i in ks:
        ct = 0
        for j in i[1:]:
            if switch[j-1] == '1':
                ct += 1
        if ct % 2 != p[idx]:
            flg = 0
            break
        idx += 1
    if flg:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
read = input 
rn = lambda :list(map(int, read().split()))
n,m = rn()
S = []
for i in range(m):
    ks = rn()
    s = ks[1:]
    s = list(map(lambda x:x-1, s))
    S.append(s)
P = rn()
ans = 0
for i in range(1<<n):
    flag = True
    for p, s in zip(P,S):
        cnt = 0
        for swc in s:
            if (1<<swc) & i >0:
                cnt +=1
        if cnt % 2 != p:
            flag = False
            break;
#     print("%x"%i, p)
    if flag: ans+=1
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))

K = [list(map(int, input().split(' '))) for i in range(M)]
P = list(map(int, input().split(' ')))

ans = 0
for i in range(2 ** N) :
    add = True
    for j in range(M) :
        k = K[j]
        cnt = 0
        for s in k[1:] :
            if (1 << (s - 1)) & i > 0 :
                cnt += 1
        if cnt % 2 == P[j] :
            pass
        else :
            add = False
            break
    if add :
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
def inpl():
    return list(map(int, input().split()))


def isAllOn(pattern):
    for i, s in enumerate(S):
        on = 0
        for k in s[1:]:
            if (pattern >> (k - 1)) % 2 == 1:
                on += 1
        # print(pattern, i, on)
        if on % 2 != P[i]:
            return False
    return True


N, M = inpl()

S = [inpl() for i in range(M)]
P = inpl()

ans = 0
for pattern in range(2**N):
    if isAllOn(pattern) is True:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
memo = []
ans = 0
for i in range(m):
    a = list(map(int, input().split()))
    b = [0]*n
    for j in range(a[0]):
        b[a[j+1]-1] = 1
    memo.append(b)
clear = list(map(int, input().split()))
for i in range(2 ** n):
    now = []
    for j in range(n):
        now.append((i // (2 ** j)) % 2)
    for j in range(m):
        t = 0
        for k in range(n):
            t += memo[j][k] * now[k]
        if t % 2 != clear[j]:
            break
    else:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import itertools

wl, gl = tuple(map(int,input().split()))

class G:
    def __init__(self):
        self.kantis = [False for _ in range(wl)]
        self.mo = 0
    def match(self,wstates):
        re = 0
        for kanti,wstate in zip(self.kantis,wstates):
            if kanti and wstate:
                re += 1
        return re % 2 == self.mo

gs = []
for gi in range(gl):
    g = G()
    kantiwis = list(map(int,input().split()))
    kantiwis = kantiwis[1:]
    for kantiwi in kantiwis:
        kantiwi -= 1
        g.kantis[kantiwi] = True
    gs.append(g)

mos = list(map(int,input().split()))
for gi in range(gl):
    gs[gi].mo=mos[gi]


re = 0
for wstates in itertools.product([True,False], repeat=wl):
    ok = True
    for g in gs:
        if not g.match(wstates):
            ok = False
            break
    if ok:
        re += 1

print(re)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def solve():
    n, m = map(int, input().split())
    ks = []
    for i in range(m):
        tks = [int(j) for j in input().split()]
        ks.append(tks[1:])
    ps = [int(i) for i in input().split()]
    ans = 0
    for i in range(2 ** n):
        t = []
        idx = 1
        while i > 0:
            if i & 1 == 1:
                t.append(idx)
            idx += 1
            i >>= 1
        for j in range(m):
            cnt = 0
            for k in ks[j]:
                if k in t:
                    cnt += 1
            if cnt % 2 != ps[j]:
                break
        else:
            ans += 1
    print(ans)

if __name__=="__main__":
    solve()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ks = [list(map(int, input().split())) for i in range(m)]
p = list(map(int, input().split()))
flag = [False for i in range(n)]
ans = 0
for bit in range(1 << n):
    a = True

    for i in range(n):
        if bit & (1 << i):
            flag[i] = True
        else:
            flag[i] = False
    for i in range(m):
        count = 0
        for j in range(len(ks[i])):
            if j == 0:
                continue
            if flag[ks[i][j] - 1]:
                count += 1
        if count % 2 != p[i]:
            a = False
    if a:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
s = []
for i in range(m):
    si = list(map(int, input().split()))
    s.append(si)
p = list(map(int, input().split()))

ans = 0
for i in range(2 ** n):
    x = format(i, '0' + str(n) +'b')
    for j in range(m):
        q = 0
        for idx in s[j][1:]:
            if x[idx - 1] == '1':
                q += 1
        if q % 2 != p[j]:
            ans -= 1
            break
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
from itertools import product,chain
from functools import reduce
from operator import xor
N,M = map(int,input().split())

lights = list(sorted(map(int,input().split()[1:])) for _ in range(M))
P = list(map(int,input().split()))

cnt = 0
for c in product((False,True), repeat=N):
    if all(not reduce(xor, (c[i-1] for i in l))^p for l,p in zip(lights, P)):
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
N, M = [int(c) for c in input().split()]

s = []
for i in range(M):
    s.append([int(c) for c in input().split()][1:])

p = [int(c) for c in input().split()]

sw = [0]*N
n = [0]

def f(i):
    if i==N:
        n[0]+=check()        
    else:
        sw[i]=0
        f(i+1)
        sw[i]=1
        f(i+1)

def check():
    all_light = True
    for s_m, p_m in zip(s,p):
        d=0
        for ss in s_m:
            d += sw[ss-1]
        if d%2 != p_m:
            all_light = False
            break
    return 1 if all_light else 0
            
f(0)

print(n[0])

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
k=[list(map(int,input().split()))for i in range(m)]
p=list(map(int,input().split()))
d=0
for i in range(2**n):
    t=bin(i)[2:]
    t="0"*(n-len(t))+t
    for j in range(m):
        c=0
        for h in range(1,k[j][0]+1):
            if t[k[j][h]-1]=="1":
                c+=1
        else:
            if c%2!=p[j]:
                break
    else:
        d+=1
print(d)

Submission status: Accepted

Solution code:
import itertools


n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
p = list(map(int, input().split()))
ans = 0

for pattern in itertools.product(range(2), repeat=n):
    power = [0]*m
    for i in range(len(pattern)):
        if pattern[i] == 1:
            for j in range(len(info)):
                if i + 1 in info[j][1:]:
                    power[j] += 1
    for i in range(m):
        if power[i] % 2 != p[i]:
            break
    else:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N, M = map(int, input().rstrip().split(' '))

S = []
for _ in range(M):
    S.append(list(map(int, input().rstrip().split(' ')))[1:])
P = list(map(int, input().rstrip().split(' ')))

count = 0
for i in range(2 ** N):
    temp = i
    X = []
    for j in range(N):
        X.append(temp // (2 ** (N-j-1)))
        temp -= X[-1] * 2 ** (N-j-1)
    
    temp_count = 0
    for j, s in enumerate(S):
        if sum([X[temp_s-1] for temp_s in s]) % 2 == P[j]:
            temp_count += 1
            
    if temp_count == M:
        count += 1

print(count)

Submission status: Accepted

Solution code:
from collections import Counter, deque, defaultdict
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys

sys.setrecursionlimit(10000)
INF = float('inf')

N, M = list(map(int, input().split()))
S = [list(map(int, input().split()))[1:] for _ in range(M)]
P = list(map(int, input().split()))

ans = 0
for pp in itertools.product([True, False], repeat=N):
    a = np.array((False,) + pp)
    ok = True
    for s, p in zip(S, P):
        if sum(a[s]) % 2 != p:
            ok = False
            break
    if ok:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def readInts():
  return [int(s) for s in input().split(" ")]

def is_on(L, i):
  if L & (1 << (i-1)):
    return True
  else:
    return False

def is_ok(L, s, p):
  c = 0
  for i in s:
    if is_on(L, i):
      c += 1
  return (c % 2) == p

def main():
  N, M = readInts()
  S = []
  for _ in range(M):
    data = readInts()
    S.append(data[1:])
  P = readInts()

  ans = 0
  for L in range(2**N):
    ok = True
    for i, s in enumerate(S):
      if not is_ok(L, s, P[i]):
        ok = False
        break
    if ok:
      ans += 1

  print(ans)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, m = li()

switch = []
for _ in range(m):
    s = list(li_())
    switch.append(s[1:])

p = list(li())

ans = 0
ok = True

for bit in range(1<<n):
    ok = True
    for i, swi in enumerate(switch):
        flag = 0
        for swij in swi:
            if bit & (1<<swij):
                flag += 1

        if flag % 2 != p[i]:
            ok = False

    if ok:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
ks = [list(map(int,input().split())) for _ in range(m)]
P = list(map(int,input().split()))

ans = 0
for p in range(2**n):
    num = format(p,'0'+str(n)+'b')
    light = 0
    for temp,i in zip(ks,range(1,m+1)):
        on = 0
        for s in temp[1:]:
            if num[s-1] == "1":
                on += 1
        if on%2 == P[i-1]:
            light += 1
    if light == m:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# n = int(input())
# 
# s = input()


N, M = map(int, input().split())
k = [0]*(M+1)
s = [0]*(M+1)
for i in range(1, M+1):
    l = list(map(int, input().split()))
    k[i] = l[0]
    s[i] = l[1:]
p = [0] + list(map(int, input().split()))

ans = 0
for i in range(1<<N):
    dp = [0]*(N+1)
    ind = i
    for k in range(N):
        if ind % 2 == 1:
            dp[k+1] = 1
        ind //= 2

    flag = True
    for k in range(1, M+1):
        a = 0
        for e in s[k]:
            a = (a + dp[e]) % 2
        if a != p[k]:
            flag = False
            break

    if flag:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
light = []
for i in range(M):
    light.append(list(map(int, input().split())))
p = list(map(int, input().split()))
ans = 0
for i in range(2**N):
    switch = "{:010b}".format(i)
    switch = switch[(10-N):]
    flag = 1
    for j in range(M):
        count = 0
        for k in range(light[j][0]):
            if switch[light[j][k+1]-1] == "1":
                count += 1
        if count % 2 == p[j]:
            continue
        else:
            flag = 0
            break
    if flag == 1:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = [[0]*N for _ in range(M)]

for i in range(M):
    k, *S = map(int, input().split())
    for s in S:
        X[i][s-1] = 1

P = list(map(int, input().split()))
ans = 0
for i in range(1<<N):
    for j in range(M):
        s = P[j]
        for k in range(N):
            if (i & (1<<k)) and X[j][k]:
                s ^= 1
        if s:
            break
    else:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
ks = [list(map(int, input().split())) for _ in range(M)]
p = list(map(int, input().split()))

Ans = 0
for i in range(pow(2, N)):
  for j in range(len(ks)):
    c = i
    t = 0
    for k in range(N):
      n = c % 2
      if n == 1 and (k + 1) in ks[j][1:]:
        t += 1
      c //= 2
    if t % 2 != p[j]:
      break
  else:
    Ans += 1

print(Ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(n, m, s, p):
    ret = 0
    #print(s)
    #print(p)
    count = 0
    for bits in range(2 ** n):
        ok = True
        for j in range(m):
            count = 0
            for i in s[j]:
                if (bits & (1 << (i - 1))) > 0:
                    count += 1
            #        print(count)
            #        print('jk')
            #    print(bits, j, count)
            #print('--- count : ', count, j, p[j])
            #print(bits, j, count, p[j])
            if count % 2 != p[j]:
                ok = False
                break
        #print(bits)
        #print(ok)
        #print()
        if ok:
            ret += 1
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    s = [[] for _ in range(M)]
    for i in range(M):
        n = int(next(tokens))
        for j in range(n):
            s[i].append(int(next(tokens)))
    p = []
    for i in range(M):
        p.append(int(next(tokens)))
    solve(N, M, s, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def rec(s_status, cp, con, p, N, M):
    if cp == N:
        for i in range(M):
            sum_s = 0
            for s in con[i]:
                sum_s += int(s_status[s - 1])
            if sum_s % 2 != p[i]:
                return 0
        else:
            return 1
    else:
        res = 0
        res += rec(s_status, cp + 1, con, p, N, M)
        s_status[cp] = True
        res += rec(s_status, cp + 1, con, p, N, M)
        s_status[cp] = False
        return res


def main():
    N, M = map(int, input().split())
    s_status = [False] * N

    con = [[] for _ in range(M)]
    for i in range(M):
        data = list(map(int, input().split()))
        con[i] = data[1:]

    p = list(map(int, input().split()))

    print(rec(s_status, 0, con, p, N, M))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ks = []
for i in range(m):
    inp = list(map(int, input().split()))
    ks.append(inp[1:])
p = list(map(int, input().split()))

ans = 0
for i in range(1<<n):
    on_all = True
    for j in range(m):
        n_on = 0
        for s in ks[j]:
            if i&(1<<(s-1)):
                n_on += 1
        if not n_on%2 == p[j]:
            on_all = False
    if on_all:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N,M =map(int,input().split())
lis =[]
for i in range(M):
    S =[int(i) for i in input().split()]
    k =S.pop(0)
    foo = 0
    for i in S:
        foo += 2**(i-1)
    lis.append(foo)

ans = 0
p = [int(i)for i in input().split()]
for i in range(2**N):
    sta =True
    for n,j in enumerate(lis):
        if bin(i&j)[2:].count("1")%2 !=p[n]:
            sta = False
    if sta:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc128/tasks/abc128_c

import itertools
from collections import Counter
from collections import defaultdict
import bisect

def main():
    N, M = map(int, input().split())

    S = []
    for _ in range(M):
        ks = list(map(int, input().split()))
        S.append(ks[1:])

    P = list(map(int, input().split()))

    # print(S)
    # print(P)

    ans = 0
    # Bit 全探索
    pattern = 1 << N
    for i in range(pattern):
        scope = []
        for j in range(N):
            if (i >> j) & 1:
                scope.append(j + 1)

        if len(scope) > 0:
            turn_on = True
            for j in range(len(S)):
                switches = S[j]
                c = 0
                for states in scope:
                    if states in switches:
                        c += 1
                if c % 2 != P[j]:
                    turn_on = False
                if not turn_on:
                    break
            if turn_on:
                # print('ok = {}'.format(scope))
                ans += 1
        else:
            turn_on = True
            for j in range(len(S)):
                if P[j] != 0:
                    turn_on = False
            if turn_on:
                ans += 1


    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
n,m=map(int,input().split())
L=[]
for i in range(m):
  a,*l=map(int,input().split())
  L+=[l]
S=list(map(int,input().split()))
ans=0
for i in itertools.product([0,1], repeat=n):
  f=0
  for l,s in zip(L,S):
    ls=0
    for ll in l:
      ls+=i[ll-1]
    if ls%2==s:
      f+=1
  if f==m:
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



def bin_cnt(n):
  ret = 0
  while n > 0:
    if n%2==1:
      ret += 1
    n = n//2
  return ret


N,M = map(int, input().split())
Sn = []
for i in range(M):
  ln = list(map(int, input().split()))
  s_bin = 0
  for l in ln[1:]:
    s_bin += 2**(l-1)
  Sn.append([0,s_bin])
Pn = list(map(int, input().split()))
for i in range(M):
  Sn[i][0] = Pn[i]

ans = 0
for i in range(2**N):
  chk = True
  for s in Sn:
    if s[0] != bin_cnt(i&s[1])%2:
      chk = False
  if chk:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
ks = [list(map(int, input().split())) for i in range(m)]
p=list(map(int, input().split()))
ans=0
for i in range(1<<n):
    val=[]
    tmp=i
    for j in range(n):
        val.append(tmp%2)
        tmp//=2
    if all(sum(val[k-1] for k in ks[j][1:])%2==p[j] for j in range(m)):
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')
mod = 10**9+7


def INT_(n): return int(n)-1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [input() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    N, M = MI()
    switch_pat = []
    for i in range(M):
        k, *s = MI()
        switch_pat.append(s)
    p = LI()
    ans = 0
    for bit in range(2 ** N):
        frag = False
        for m in range(M):
            cnt = 0
            for switch in switch_pat[m]:
                if 1 << (switch-1) & bit:
                    cnt += 1
            if cnt % 2 == p[m]:
                continue
            else:
                frag = True
                break
        if not frag:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
k = []
s = []
for i in range(M):
    l = list(map(int,input().split()))
    k.append(l[0])
    s.append(l[1:])
p = list(map(int,input().split()))
ans = 0
for n in range(2**N):
    n = bin(n)[2:]
    n = '0'*(N-len(n))+n
    f = True
    for i in range(M):
        sum = 0
        for j in range(k[i]):
            if n[-s[i][j]] == '1':
                sum += 1
        if sum%2 != p[i]:
            f = False
    if f:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
S = [0] * N
L = [0] * M
for i in range(M):
  L[i] = [int(x) - 1 for x in input().split()]
P = [int(x) for x in input().split()]
cnt = 0
for i in range(2 ** N):
  n = i
  for j in range(N):
    S[j] = n % 2
    n //= 2
  for k in range(M):
    L[k][0] = 0
    for l in L[k][1:]:
      L[k][0] ^= S[l]
  light = True
  for k in range(M):
    light &= (L[k][0] == P[k])
  cnt += 1 if light else 0
print(cnt)

Submission status: Accepted

Solution code:
import itertools


def is_bright(switch, status, p):
    count = 0
    for s in switch:
        if status[s - 1]:
            count += 1
            
    return count % 2 == p


n, m = map(int, input().split())
switches = list()
for i in range(m):
    tmp = input().split()
    switches.append(list(map(int, tmp[1:])))

ps = list(map(int, input().split()))

count = 0
for status in itertools.product(*[[True, False]] * n):
    for switch, p in zip(switches, ps):
        if not is_bright(switch, status, p):
            break
    
    else:
        count += 1

print(count)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
s = [list(map(int, input().split())) for i in range(m)]
p = list(map(int, input().split()))

x = []
for i in range(2**n):
    x.append(format(i,'0b').zfill(n))

ans = 0
for i in range(2**n):
    for j in range(m):
        b = 0
        for k in range(len(s[j])-1):
            if x[i][s[j][k+1]-1]=='1':
                b+=1
        if b%2!=p[j]:
            break
        if j==m-1:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

############
############
############

N, M = s_inpl()
ks = []
for _ in range(M):
    k = l_inpl()
    ks.append(k[1:])

ps = l_inpl()
cnt = 0
for i in range(2**N):
    on_ss = []
    for j in range(N):
        if ((i>>j) & 1):
            on_ss.append(j+1)
    # print("JJJ")
    flag = True
    for k, p in zip(ks, ps):
        if len(set(k)&set(on_ss)) % 2 != p:
            flag = False
            break
    if flag:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
N,M=list(map(int,input().strip().split(' ')))


dic_connect={}

for _ in range(M):
    X=list(map(int,input().strip().split(' ')))
    X=X[1:]
    for x in X:
        if x in dic_connect:
            dic_connect[x]+=[_+1]
        else:
            dic_connect[x]=[_+1]    


P=[-1]+list(map(int,input().strip().split(' ')))
count=0

def check(s,N,M):
    count_bulbs=[0]*(M+1)
    for i in range(len(s)):
        if s[i]=='1':
            switch=i+1
            if switch in dic_connect:
                for b in dic_connect[switch]:
                    count_bulbs[b]+=1
    
    for i in range(1,M+1):
        if (count_bulbs[i]-P[i])%2!=0:
            return 0
    
    return 1

#print(dic_connect)

for i in range(2**N):
    s="0"*(N-len(bin(i)[2:]))+bin(i)[2:]
    count+=check(s,N,M)
print(count)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
s = []
for i in range(m):
  l = list(map(int,input().split()))[1:]
  s.append([t-1 for t in l])
#print(s)
#quit()
p = list(map(int,input().split()))
ans = 0  
for k in range(2**n):
  f = 1
  b = format(k, '010b')[::-1]
  for i in range(m):
    c = p[i]
    for j in s[i]:
      c += int(b[j])
    if c%2 == 1:
      f = 0
      break
  ans += f
      
print(ans)