#P8981. 「DROI」Round 1 距离

    ID: 10013 远端评测题 1000~3000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>树形数据结构洛谷原创O2优化树形 DP树的直径

「DROI」Round 1 距离

Background

No distance is impossible to cross.

Problem Description

Given a tree GG, define the distance dis(u,v)\operatorname{dis}(u,v) between two nodes u,vu, v as the number of nodes on the path between them.

If for two nodes u,vu, v on the tree, the following holds: $\forall x \in G,\operatorname{dis}(u,x) \leq \operatorname{dis}(u,v)$ and $\operatorname{dis}(v,x) \leq \operatorname{dis}(u,v)$, then we call the unordered pair (u,v)(u,v) an extremely far pair.

Also, for a node xx in the tree GG, its weight vxv_x is defined as: the number of extremely far pairs whose shortest path passes through xx.

Given the tree GG, compute xGvxkmod998244353\sum\limits_{x \in G}{v_x^k} \bmod 998244353, where kk is a given constant and k[1,2]k \in [1,2].

Input Format

The first line contains two integers n,kn, k, representing the number of nodes in the tree GG and the given constant.

The next n1n - 1 lines each contain two integers u,vu, v, indicating that there is an edge between node uu and node vv.

Output Format

Output one integer in one line, representing the answer.

2 1
1 2

2
5 2
1 2
1 3
4 1
5 1

72

Hint

Sample Explanation #1

(1,2)(1,2) is an extremely far pair, so the weights of nodes 11 and 22 are both 11, and 11+11=21^1 + 1^1 = 2.


Sample Explanation #2

The extremely far pairs are (2,3),(2,4),(2,5),(3,4),(3,5),(4,5)(2,3),(2,4),(2,5),(3,4),(3,5),(4,5), so the answer is 4×32+62=724 \times 3^2 + 6^2 = 72.


Constraints

Test Point ID 11 22 33 454 \sim 5 66 77 898 \sim 9 1010
nn 300300 20002000 10510^5 5×1065 \times 10^6 10510^5 5×1065 \times 10^6
kk 11 22 11 22 11 22

For 100%100\% of the testdata, n5×106n \leq 5 \times 10^6 and 1k21 \leq k \leq 2.

The input size is large, so please use a fast input method.

Translated by ChatGPT 5