C - Switches Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

on と off の状態を持つ N 個の スイッチと、M 個の電球があります。スイッチには 1 から N の、電球には 1 から M の番号がついています。

電球 ik_i 個のスイッチに繋がっており、スイッチ s_{i1}, s_{i2}, ..., s_{ik_i} のうち on になっているスイッチの個数を 2 で割った余りが p_i に等しい時に点灯します。

全ての電球が点灯するようなスイッチの on/off の状態の組み合わせは何通りあるでしょうか。

制約

  • 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_i0 または 1
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

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

出力

全ての電球が点灯するようなスイッチの on/off の状態の組み合わせの個数を出力せよ。


入力例 1

2 2
2 1 2
1 2
0 1

出力例 1

1
  • 電球 1 は、次のスイッチのうち偶数個が on の時に点灯します: スイッチ 1, 2
  • 電球 2 は、次のスイッチのうち奇数個が on の時に点灯します: スイッチ 2

(スイッチ 1、スイッチ 2) の状態としては (on,on),(on,off),(off,on),(off,off) が考えられますが、このうち (on,on) のみが条件を満たすので、1 を出力してください。


入力例 2

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

出力例 2

0
  • 電球 1 は、次のスイッチのうち偶数個が on の時に点灯します: スイッチ 1, 2
  • 電球 2 は、次のスイッチのうち偶数個が on の時に点灯します: スイッチ 1
  • 電球 3 は、次のスイッチのうち奇数個が on の時に点灯します: スイッチ 2

電球 2 を点灯させるためには スイッチ 1 が off, 電球 3 を点灯させるためにはスイッチ 2 が on である必要がありますが、この時電球 1 は点灯しません。

よって、全ての電球が点灯するようなスイッチの on/off の状態の組み合わせは存在しないので、0 を出力してください。


入力例 3

5 2
3 1 2 5
2 2 3
1 0

出力例 3

8

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