#abc222e. E - Red and Blue Tree

E - Red and Blue Tree

Score : 500500 points

问题描述

给定一棵具有 NN 个顶点的树,一个包含 MM 个数的序列 A=(A1,,AM)A=(A_1,\ldots,A_M),以及一个整数 KK

顶点按 11NN 编号,第 ii 条边连接顶点 UiU_iViV_i

我们将这棵树的 N1N-1 条边分别涂成红色或蓝色。在所有 2N12^{N-1} 种涂色方式中,找出满足以下条件的方式的数量,并对 998244353998244353 取模的结果。

条件:

  • 首先,在顶点 A1A_1 上放置一个棋子,按照顺序对于每个 i=1,,M1i=1,\ldots,M-1,沿着最短路径从顶点 AiA_i 移动到顶点 Ai+1A_{i+1}
  • 在完成所有这些移动后,令 RR 表示棋子经过红色边的次数,BB 表示经过蓝色边的次数,则要求满足 RB=KR-B=K

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Given are a tree with NN vertices, a sequence of MM numbers A=(A1,,AM)A=(A_1,\ldots,A_M), and an integer KK.
The vertices are numbered 11 through NN, and the ii-th edge connects Vertices UiU_i and ViV_i.

We will paint each of the N1N-1 edges of this tree red or blue. Among the 2N12^{N-1} such ways, find the number of ones that satisfies the following condition, modulo 998244353998244353.

Condition:
Let us put a piece on Vertex A1A_1, and for each i=1,,M1i=1,\ldots,M-1 in this order, move it from Vertex AiA_i to Vertex Ai+1A_{i+1} along the edges in the shortest path. After all of these movements, RB=KR-B=K holds, where RR and BB are the numbers of times the piece traverses a red edge and a blue edge, respectively.

Constraints

  • 2N10002 \leq N \leq 1000
  • 2M1002 \leq M \leq 100
  • K105|K| \leq 10^5
  • 1AiN1 \leq A_i \leq N
  • 1Ui,ViN1\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:

NN MM KK

A1A_1 A2A_2 \ldots AMA_M

U1U_1 V1V_1

\vdots

UN1U_{N-1} VN1V_{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 11-st and 33-rd edges red and the 22-nd edge blue, the piece will traverse the following numbers of red and blue edges:

  • 00 red edges and 11 blue edge when moving from Vertex 22 to 33,
  • 00 red edges and 11 blue edge when moving from Vertex 33 to 22,
  • 11 red edge and 00 blue edges when moving from Vertex 22 to 11,
  • 22 red edges and 11 blue edge when moving from Vertex 11 to 44,

for a total of 33 red edges and 33 blue edges, satisfying the condition.

Figure

Another way to satisfy the condition is to paint the 11-st and 33-rd edges blue and the 22-nd edge red. There is no other way to satisfy it, so the answer is 22.

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