luogu#P5206. [WC2019] 数树

    ID: 4178 远端评测题 4000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2019树形 DP组合数学生成函数拉格朗日反演WC

[WC2019] 数树

Background

The white rabbit likes trees.

The white cloud likes counting.

There are nn mice. The white rabbit uses n1n - 1 blue ropes to connect them into a tree; each blue rope connects two mice. The white cloud uses n1n - 1 red ropes to connect them into a tree; each red rope connects two mice.

The white cloud wants to assign a number to each mouse. This number can be any integer in [1,y][1, y].

The white rabbit gives the white cloud a requirement: for two mice p,qp, q, if there exists a path connecting these two mice that belongs to both trees, then pp and qq must be assigned the same integer.
“Having a path that belongs to both trees” means: there exists a sequence (a1=p,a2,,am=q)(a_1 = p, a_2, \cdots , a_m = q) such that for all i[1,m1]i \in [1, m - 1], aia_i and ai+1a_{i+1} are directly connected by both a red rope and a blue rope.

The white cloud wants to know: how many different assignment schemes are there?

The mice keep struggling, trying to break free from the ropes. Before the white cloud can figure it out, the mice bite off all the red ropes.

The white rabbit is somewhat annoyed, but still wants to know the answer. So he asks the white cloud: over all possible connection schemes of the red ropes, what is the total sum of the answers (that is, sum the number of assignment schemes over all red-rope connection schemes)?

The mice keep struggling, trying to break free from the ropes. Before the white cloud can figure it out, the mice bite off the blue ropes as well.

The white rabbit is somewhat annoyed, but still wants to know the answer. So he asks the white cloud: over all possible connection schemes of both the red ropes and the blue ropes, what is the total sum of the answers (that is, sum the number of assignment schemes over all red-rope and blue-rope connection schemes)? Two schemes are different if and only if there exists at least one pair of mice such that the ropes directly connecting these two mice are different in the two schemes (there are 4 possibilities for ropes between two mice: no rope directly connects them, only a red rope directly connects them, only a blue rope directly connects them, and both colors of ropes directly connect them).

The white cloud cries.

Problem Description

This problem contains three questions:

  • Question 0: The shapes of two trees with nn nodes are given (both trees have nodes labeled from 11 to nn). The first tree is the red tree, and the second tree is the blue tree. You need to assign each node an integer in [1,y][1, y] such that for any two nodes p,qp, q, if there exists a path (a1=p,a2,,am=q)(a_1 = p, a_2, \cdots , a_m = q) that belongs to both trees, then p,qp, q must be assigned the same number. Find the number of assignment schemes.
    • The definition of “there exists a path that belongs to both trees” is the same as in the “Background”.
  • Question 1: The blue tree is given. For all nn2n^{n-2} choices of the red tree, compute the sum of the answers to Question 0.
  • Question 2: For all nn2n^{n-2} choices of the blue tree, compute the sum of the answers to Question 1.

Hint: There are nn2n^{n-2} different trees on nn nodes.

In different test points, you may need to answer different questions. We use op\text{op} to denote the index of the question you need to answer (corresponding to 0, 1, 2 above).

Since the answer may be very large, you only need to output it modulo 998,244,353998, 244, 353.

Input Format

The first line contains three integers n,y,opn, y, \text{op} separated by spaces.
If op=0\text{op} = 0, then the next 2×(n1)2 \times (n - 1) lines follow: the first (n1)(n - 1) lines each describe a blue rope, and the next (n1)(n - 1) lines each describe a red rope.
If op=1\text{op} = 1, then the next (n1)(n - 1) lines follow, each describing a blue rope.
If op=2\text{op} = 2, then there is no further input.
Each rope description line contains two integers separated by a space, representing the indices of the two mice connected by this rope. The mice are numbered starting from 11.

Output Format

Output one integer, representing the answer modulo 998,244,353998, 244, 353.

3 2 0
1 2
2 3
1 2
2 3
2

3 2 1
1 2
2 3
10
3 2 2

30

Hint

Sample Explanation 1

The two trees are the same, so any two nodes must be assigned the same number. The number of schemes is 22.

Sample Explanation 2

There are three possible red trees:

  1. Contains ropes (1,2)(1, 2) (meaning the rope connecting mice 11 and 22, similarly below), (2,3)(2, 3). Then any two mice must be assigned the same number, so the answer to Question 0 is 22.
  2. Contains ropes (1,2)(1, 2), (1,3)(1, 3). Then mice 11 and 22 must be assigned the same number, so the answer to Question 0 is 2×2=42 \times 2 = 4.
  3. Contains ropes (2,3)(2, 3), (1,3)(1, 3). Then node 22 and node 33 must be assigned the same number, so the answer to Question 0 is 2×2=42 \times 2 = 4.

Therefore, the answer to Question 1 is 2+4+4=102 + 4 + 4 = 10.

Sample Explanation 3

There are three possible blue trees. It is not hard to see that for each blue tree, the computed answer to Question 1 is 1010. So the answer is 10×3=3010 \times 3 = 30.

::cute-table{tuack}

Question Type (op)(\mathrm{op}) Test Point ID nn yy Points per Test (CTT) Points per Test (Non-CTT)
0 1 10\le 10 No special restriction 2 18
^ 2 105\le 10^{5} ^ ^ 5
3 ^ ^
1 4 =3=3 1 4
^ 5 =5=5 ^ ^
6 500\le 500 6
7 ^ ^
8 5000\le 5000
9 ^
10 105\le 10^{5} =1=1 1
11 ^ No special restriction 5 2
12 ^ ^
13
14
2 15 =3=3 1 4
^ 16 =10=10 ^ ^
17 500\le 500 6
18 ^ ^
19 5000\le 5000
20 ^
21 105\le 10^{5} =1=1 1
22 ^ No special restriction 5 2
23 ^
24
25

To optimize your reading experience, we placed the Test Point ID in the middle of the table; please pay attention to this.

All test points satisfy $3 \leq n \leq 10^{5}, 1 \leq y<998244353, o p \in\{0,1,2\}$.

Luogu scores according to CTT points

Translated by ChatGPT 5