Tree GrowingAI (CI翻译)
Problem Statement
You are given a tree with $N$ vertices numbered $1$ to $N$. Here, $N$ is at least $2$. The $i$-th edge $(1\le i\le N-1)$ connects vertices $u_i$ and $v_i$.
Perform the following operation on this tree exactly $K$ times. The $k$-th operation $(1\le k\le K)$ is as follows.
- Choose one of the
$N-2+k$edges of the tree at that point uniformly at random. Let$u$and$v$be the vertices connected by the chosen edge. Prepare a new vertex$N+k$, add edges connecting vertices$u$and$N+k$and connecting vertices$v$and$N+k$, then delete the chosen edge.
Note that for $k=1,2,\ldots,K$, the tree will have $N+k$ vertices after the $k$-th operation.
Find the expected value, modulo $998244353$, of $\displaystyle \sum_{1\le i < j\le N+K} \text{dist}(i,j)$ after $K$ operations. Here, $\text{dist}(i,j)$ denotes the distance between vertices $i$ and $j$ in the tree.
You are given $T$ test cases; solve each of them.
Definition of expected value $\ \text{mod}\ 998244353$ It can be proved that the expected value to be found is always a rational number. Moreover, under the constraints of this problem, when this rational number is expressed as an irreducible fraction $\displaystyle \frac{P}{Q}$, it can be proved that $Q \neq 0 \bmod 998244353$. Thus, there is a unique integer $R$ satisfying $R \times Q \equiv P \bmod 998244353, 0 \leq R \lt 998244353$. Find this $R$.
Constraints
$1\le T$$2\le N$- The sum of
$N$over all test cases is at most$3\times 10^5$. $1\le K$- The sum of
$K$over all test cases is at most$10^6$. $1\le u_i < v_i \le N$- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Output the answers for the test cases in order, separated by newlines.
{"titleZh":"树的生长","bodyZh":"## 题目描述\n\n给定一棵包含 $N$ 个顶点的树,顶点编号为 $1$ 至 $N$。其中,$N$ 至少为 $2$。第 $i$ 条边 $(1\\le i\\le N-1)$ 连接顶点 $u_i$ 和 $v_i$。\n\n对这棵树恰好执行 $K$ 次以下操作。第 $k$ 次操作 $(1\\le k\\le K)$ 如下所示。\n\n- 等概率随机选择此时树中的 $N-2+k$ 条边之一。设被选中的边连接顶点 $u$ 和 $v$。创建一个新顶点 $N+k$,添加连接顶点 $u$ 与 $N+k$ 的边,以及连接顶点 $v$ 与 $N+k$ 的边,然后删除被选中的边。\n\n注意,对于 $k=1,2,\\ldots,K$,第 $k$ 次操作后树中将有 $N+k$ 个顶点。\n\n求执行 $K$ 次操作后,$\displaystyle \\sum_{1\\le i < j\\le N+K} \\text{dist}(i,j)$ 的期望值模 $998244353$ 的结果。其中,$\\text{dist}(i,j)$ 表示树中顶点 $i$ 和顶点 $j$ 之间的距离。\n\n给定 $T$ 组测试用例,请分别求出每组测试用例的答案。\n\n期望值 $\ \\text{mod}\ 998244353$ 的定义\n\n可以证明,所求的期望值总是一个有理数。此外,在本题的约束条件下,若将该有理数表示为最简分数 $\displaystyle\\frac{P}{Q}$,可以证明 $Q \\neq 0 \\bmod 998244353$。因此,满足 $R \\times Q \\equiv P \\bmod 998244353, 0 \\leq R \\lt 998244353$ 的整数 $R$ 唯一存在。请求出这个 $R$。\n\n## 约束条件\n\n- $1\\le T$\n- $2\\le N$\n- 所有测试用例的 $N$ 之和不超过 $3\\times 10^5$。\n- $1\\le K$\n- 所有测试用例的 $K$ 之和不超过 $10^6$。\n- $1\\le u_i < v_i \\le N$\n- 给定的图是一棵树。\n- 所有输入值均为整数。\n\n## 输入格式\n\n输入从标准输入给出,格式如下:\n\n\n\n## 输出格式\n\n按测试用例顺序输出答案,每个答案占一行。"}AI (CI翻译)\nT\n\\text{case}_1\n\\text{case}_2\n\\vdots\n\\text{case}_T\n`\n\n每组测试用例的格式如下:\n\n`\nN K\nu_1 v_1\nu_2 v_2\n\\vdots\nu_{N-1} v_{N-1}\n
样例
3
4 2
1 2
1 3
1 4
4 5
1 2
2 3
3 4
6 13
1 2
1 3
2 4
2 5
3 6
499122208
120
713032723
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。