atcoder#ARC121F. [ARC121F] Logical Operations on Tree
[ARC121F] Logical Operations on Tree
Score : points
Problem Statement
Given is a tree with vertices numbered through .
The -th edge connects Vertices and .
Snuke will label each vertex with 0
or 1
and each edge with AND
or OR
.
Among the ways to label the vertices and edges, find the number of ways that satisfy the following condition, modulo .
Condition: There exists a sequence of operations ending up with one vertex labeled 1
, where each operation consists of the steps below.
- Choose one edge and contract it. Here, let and be the labels of the erased vertices and be the label of the erased edge.
- If is
AND
, label the new vertex with ; if isOR
, label the new vertex with .
Notes
- The operation is defined as follows: $\mathrm{AND}(0,0)=(0,1)=(1,0)=0,\mathrm{AND}(1,1)=1$.
- The operation is defined as follows: .
- When contracting the edge connecting Vertex and Vertex , we merge the two vertices while removing that edge. After the contraction, there is an edge connecting the new vertex and vertex if and only if there was an edge connecting and or connecting and before the contraction.
Constraints
- All values in input are integers.
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
Output
Print the number of ways to label the tree that satisfy the condition in Problem Statement, modulo .
2
1 2
4
20
7 3
20 18
16 12
7 2
10 5
18 16
16 3
4 11
7 15
8 1
6 1
12 13
15 5
19 17
7 1
9 8
7 17
16 14
11 7
283374562
- Remember to print the count modulo .