#abc222e. E - Red and Blue Tree
E - Red and Blue Tree
Score : points
问题描述
给定一棵具有 个顶点的树,一个包含 个数的序列 ,以及一个整数 。
顶点按 到 编号,第 条边连接顶点 和 。
我们将这棵树的 条边分别涂成红色或蓝色。在所有 种涂色方式中,找出满足以下条件的方式的数量,并对 取模的结果。
条件:
- 首先,在顶点 上放置一个棋子,按照顺序对于每个 ,沿着最短路径从顶点 移动到顶点 。
- 在完成所有这些移动后,令 表示棋子经过红色边的次数, 表示经过蓝色边的次数,则要求满足 。
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
4 5 0
2 3 2 1 4
1 2
2 3
3 4
Sample Output 1
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 .
Sample Input 2
3 10 10000
1 2 1 2 1 2 2 1 1 2
1 2
1 3
Sample Output 2
0
There may be no way to paint the tree to satisfy the condition.
Sample Input 3
10 2 -1
1 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
Sample Output 3
126
Sample Input 4
5 8 -1
1 4 1 4 2 1 3 5
1 2
4 1
3 1
1 5
Sample Output 4
2
update @ 2024/3/10 09:47:32