102224: [AtCoder]ABC222 E - Red and Blue Tree
Description
Score : $500$ points
Problem Statement
Given are a tree with $N$ vertices, a sequence of $M$ numbers $A=(A_1,\ldots,A_M)$, and an integer $K$.
The vertices are numbered $1$ through $N$, and the $i$-th edge connects Vertices $U_i$ and $V_i$.
We will paint each of the $N-1$ edges of this tree red or blue. Among the $2^{N-1}$ such ways, find the number of ones that satisfies the following condition, modulo $998244353$.
Condition:
Let us put a piece on Vertex $A_1$, and for each $i=1,\ldots,M-1$ in this order, move it from Vertex $A_i$ to Vertex $A_{i+1}$ along the edges in the shortest path. After all of these movements, $R-B=K$ holds, where $R$ and $B$ are the numbers of times the piece traverses a red edge and a blue edge, respectively.
Constraints
- $2 \leq N \leq 1000$
- $2 \leq M \leq 100$
- $|K| \leq 10^5$
- $1 \leq A_i \leq N$
- $1\leq U_i,V_i\leq N$
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $M$ $K$ $A_1$ $A_2$ $\ldots$ $A_M$ $U_1$ $V_1$ $\vdots$ $U_{N-1}$ $V_{N-1}$
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 $1$-st and $3$-rd edges red and the $2$-nd edge blue, the piece will traverse the following numbers of red and blue edges:
- $0$ red edges and $1$ blue edge when moving from Vertex $2$ to $3$,
- $0$ red edges and $1$ blue edge when moving from Vertex $3$ to $2$,
- $1$ red edge and $0$ blue edges when moving from Vertex $2$ to $1$,
- $2$ red edges and $1$ blue edge when moving from Vertex $1$ to $4$,
for a total of $3$ red edges and $3$ blue edges, satisfying the condition.
Another way to satisfy the condition is to paint the $1$-st and $3$-rd edges blue and the $2$-nd edge red. There is no other way to satisfy it, so the answer is $2$.
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
Input
题意翻译
给出$n$个点的树和长度为$m$的序列$a$。现需要给每条边染成红色(red)或者蓝色(blue),要求按照$a$走的路径,经过的边数$红色−蓝色=k$,问方案数。Output
分数:500分
问题描述
给定一个有N个顶点的树,一个长度为M的整数序列A=(A_1,...,A_M),以及一个整数K。
顶点编号为1到N,第i条边连接顶点U_i和V_i。
我们要在这棵树的N-1条边中,将每一条染成红色或蓝色。在2^{N-1}种这样的方式中,找出满足以下条件的方式的数量,对998244353取模。
条件:
在顶点A_1上放置一个棋子,然后按照顺序,从顶点A_i移动到顶点A_{i+1},沿着最短路径中的边。经过所有的移动后,R-B=K成立,其中R和B分别是棋子经过红色边和蓝色边的次数。
约束条件
- 2 \leq N \leq 1000
- 2 \leq M \leq 100
- |K| \leq 10^5
- 1 \leq A_i \leq N
- 1\leq U_i,V_i\leq N
- 给定的图是一棵树。
- 输入中的所有值都是整数。
输入
输入通过标准输入给出以下格式:
$N$ $M$ $K$ $A_1$ $A_2$ $\ldots$ $A_M$ $U_1$ $V_1$ $\vdots$ $U_{N-1}$ $V_{N-1}$
输出
输出答案。
样例输入1
4 5 0 2 3 2 1 4 1 2 2 3 3 4
样例输出1
2
如果我们把第1条和第3条边涂成红色,第2条边涂成蓝色,棋子将沿着以下红色和蓝色边的次数移动:
- 当从顶点2移动到顶点3时,没有红色边和1条蓝色边,
- 当从顶点3移动到顶点2时,没有红色边和1条蓝色边,
- 当从顶点2移动到顶点1时,有1条红色边和0条蓝色边,
- 当从顶点1移动到顶点4时,有2条红色边和1条蓝色边,
总共3条红色边和3条蓝色边,满足条件。
另一种满足条件的方式是把第1条和第3条边涂成蓝色,第2条边涂成红色。没有其他方式可以满足它,所以答案是2。
样例输入2
3 10 10000 1 2 1 2 1 2 2 1 1 2 1 2 1 3
样例输出2
0
可能没有满足条件的涂色方式。
样例输入3
10 2 -1 1 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
样例输出3
126
样例输入4
5 8 -1 1 4 1 4 2 1 3 5 1 2 4 1 3 1 1 5
样例输出4
2