#abc329g. G - Delivery on Tree

G - Delivery on Tree

Score : 625625 points

问题描述

给定一棵包含 NN 个顶点的二叉树。顶点编号从 11NN,其中顶点 11 是根节点。第 ii 条边 (1iN1)(1\leq i \leq N-1) 双向连接顶点 i+1i+1 和顶点 Pi (i)P_i\ (\leq i)

在这个树上有 11 个篮子和 MM 个球。球的编号为 11MM,每个球 jj 有一个指定的起点 SjS_j终点 TjT_j。初始时,篮子为空并放置在顶点 11 处,球则放置在其各自的起点处。

你可以按任意顺序执行以下操作任意多次:

  • vv 表示当前篮子所在的顶点,执行以下任一操作:
    • 选择一条与顶点 vv 相连的边,沿该边移动篮子至相邻顶点。篮子中的球随之一起移动。
    • 选择一个起点为 vv 并且仍在顶点 vv 上的球,将其放入篮子中。此操作仅当篮子中球的数量少于 KK 个(即篮子不能容纳 K+1K+1 或更多的球)时才能执行。
    • 从篮子中选择一个终点为 vv 的球取出,并将其放在顶点 vv 处。

如果最终篮子为空并且位于顶点 11 处,同时所有球都到达了它们各自的终点,则称这样的操作序列是一个有效操作序列

由于频繁移动篮子很费力,篮子的移动路径应在返回顶点 11 之前恰好经过每条边两次。求满足存在遵循该路径的有效操作序列的路径数量模 998244353998244353 的结果。

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

Problem Statement

You are given a binary tree with NN vertices. The vertices are numbered 11 to NN, with vertex 11 being the root. The ii-th edge (1iN1)(1\leq i \leq N-1) connects vertex i+1i+1 and vertex Pi (i)P_i\ (\leq i) bidirectionally.

There are one basket and MM balls on this tree. The balls are numbered 11 to MM, and each ball jj has a designated start vertex SjS_j and goal vertex TjT_j. Initially, the basket is empty and placed at vertex 11, and the balls are placed at their respective start vertices.

You can perform the following operation any number of times in any order.

  • Let vv be the current vertex where the basket is placed. Do one of the following:
    • Choose one edge connected to vertex vv, and move the basket along that edge to the adjacent vertex. The balls inside the basket move together with it.
    • Choose a ball with the start vertex vv that is still placed at vv, and put it into the basket. This operation can only be performed if the basket contains fewer than KK balls (that is, the basket cannot contain K+1K+1 or more balls).
    • Choose a ball with the goal vertex vv from the basket and take it out, placing it at vertex vv.

A sequence of operations is called a good operation sequence if and only if the basket is eventually empty and placed at vertex 11, and the balls are eventually placed at their respective goal vertices.

Since it is tiring to move the basket many times, the basket's movement path should traverse each edge exactly twice before returning to vertex 11. Find the number, modulo 998244353998244353, of those paths such that a good operation sequence exists following that path.

Constraints

  • 2N1042\leq N \leq 10^4
  • 1M2×1051\leq M \leq 2\times 10^5
  • 1K1031\leq K \leq 10^3
  • 1Pii1\leq P_i \leq i
  • For every v (1vN)v\ (1\leq v \leq N), there are at most two ii's such that Pi=vP_i=v.
  • 1Sj,TjN1\leq S_j, T_j \leq N
  • SjTjS_j \neq T_j
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM KK

P1P_1 P2P_2 \dots PN1P_{N-1}

S1S_1 T1T_1

S2S_2 T2T_2

\vdots

SMS_M TMT_M

Output

Print the number, modulo 998244353998244353, of paths traversing every edge exactly twice before returning to vertex 11 such that a good operation sequence exists following that path.

Sample Input 1

5 2 1
1 1 3 3
2 4
5 3

Sample Output 1

1

The given graph is shown in the figure below. The circles and squares written next to the vertices represent the start and goal vertices of the balls, respectively.

Among the paths where every edge is traversed exactly twice before returning to vertex 11, there is only one path such that a good operation sequence exists following that path, which is shown below:

Here, you can construct the following good operation sequence:

  1. Move the basket to vertex 22.
  2. Put ball 11 into the basket.
  3. Move the basket to vertex 11.
  4. Move the basket to vertex 33.
  5. Move the basket to vertex 44.
  6. Take ball 11 out of the basket and place it at vertex 44.
  7. Move the basket to vertex 33.
  8. Move the basket to vertex 55.
  9. Put ball 22 into the basket.
  10. Move the basket to vertex 33.
  11. Take ball 22 out of the basket and place it at vertex 33.
  12. Move the basket to vertex 11.

Sample Input 2

5 2 2
1 1 3 3
2 4
5 3

Sample Output 2

2

The value of KK has increased by 11 from Sample Input 1. This allows you to construct a good operation sequence for the following path, in addition to the one illustrated above.

Sample Input 3

15 4 2
1 2 1 4 2 3 4 7 3 7 5 9 11 8
14 12
5 4
13 15
5 12

Sample Output 3

8

update @ 2024/3/10 01:55:10