atcoder#ABC222E. [ABC222E] Red and Blue Tree
[ABC222E] Red and Blue Tree
Score : points
Problem Statement
Given are a tree with vertices, a sequence of numbers , and an integer . The vertices are numbered through , and the -th edge connects Vertices and .
We will paint each of the edges of this tree red or blue. Among the such ways, find the number of ones that satisfies the following condition, modulo .
Condition: Let us put a piece on Vertex , and for each in this order, move it from Vertex to Vertex along the edges in the shortest path. After all of these movements, holds, where and are the numbers of times the piece traverses a red edge and a blue edge, respectively.
Constraints
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
4 5 0
2 3 2 1 4
1 2
2 3
3 4
2
If we paint the -st and -rd edges red and the -nd edge blue, the piece will traverse the following numbers of red and blue edges:
- red edges and blue edge when moving from Vertex to ,
- red edges and blue edge when moving from Vertex to ,
- red edge and blue edges when moving from Vertex to ,
- red edges and blue edge when moving from Vertex to ,
for a total of red edges and blue edges, satisfying the condition.
Another way to satisfy the condition is to paint the -st and -rd edges blue and the -nd edge red. There is no other way to satisfy it, so the answer is .
3 10 10000
1 2 1 2 1 2 2 1 1 2
1 2
1 3
0
There may be no way to paint the tree to satisfy the condition.
10 2 -1
1 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
126
5 8 -1
1 4 1 4 2 1 3 5
1 2
4 1
3 1
1 5
2