atcoder#AGC050F. [AGC050F] NAND Tree
[AGC050F] NAND Tree
Score : points
Problem Statement
There is a tree whose vertices are labelled with either or . The tree has vertices numbered through . For each , there is an edge connecting Vertex and Vertex . The label of Vertex is .
Snuke wants to repeat performing the following operation on the tree:
- Choose an edge, contract it, and label the new vertex with the NAND of the labels of two old vertices.
After performing operations, the tree ends up with a single vertex. Among ways to do so, how many will result in a vertex labelled with ? Compute the answer modulo 2.
Notes
- NAND operation is defined as follows: $NAND(0, 0) = NAND(0, 1) = NAND(1, 0) = 1, NAND(1, 1) = 0$.
- When we contract an edge between Vertex and Vertex , we remove the edge, and simultaneously merge the two vertices. There is an edge between the merged vertex and Vertex in the new tree iff there is an edge between and , or an edge between and , in the old tree.
Constraints
- The input represents a valid tree.
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
4
1 2
2 3
2 4
0 1 1 0
0
It turns out that in of all ways the final label will be , so the answer is .
4
1 2
2 3
3 4
1 1 0 1
1
20
3 15
1 12
10 16
3 19
5 20
1 9
6 13
14 19
2 4
8 11
12 16
6 20
2 17
16 18
17 19
7 10
16 20
2 20
8 20
1 0 0 1 1 0 1 1 1 0 0 1 0 1 0 1 1 1 0 0
0