#P14812. [CCPC 2024 哈尔滨站] 树上游戏
[CCPC 2024 哈尔滨站] 树上游戏
Description
There is a tree (a connected undirected graph with nodes and edges) consisting of nodes, with nodes numbered from to . Clearly, there is a unique simple path between any two nodes in the tree.
Xiaohong and Xiaolan are playing a game on this tree. In each game, both players select a random simple path from all simple paths (regardless of direction) that exist in the tree. Note that they may choose the same path. Let denote the number of edges that are common to both selected paths, and the score of the game is .
Your task is to find the expected value of the score when Xiaohong and Xiaolan play the game once, and output the result modulo (see the output format for details).
Input Format
The first line contains a positive integer (), representing the number of test cases.
For each test case, the first line contains a positive integer (), representing the number of nodes in the tree.
The next lines each contain two positive integers (), indicating that there is an edge between nodes and . The input is guaranteed to be a tree.
The sum of all over all test cases does not exceed .
Output Format
For each test case, output a single integer, representing the answer modulo .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to , where denotes the modular multiplicative inverse of modulo . In other words, output such an integer that and . It can be proved that there is exactly one which meets the condition.
2
3
1 2
2 3
5
1 2
1 5
3 2
4 2
443664158
918384806
Hint
For the first test case in the example, the answer without taking the modulo is .
Among the possible cases:
- In cases, the number of common edges between the two paths is ;
- In cases, the number of common edges between the two paths is ;
- In case, the number of common edges between the two paths is .
Therefore, the answer is $E(X^2) = \frac{2 \cdot 0^2 + 6 \cdot 1^2 + 1 \cdot 2^2}{9} = \frac{10}{9}$.
京公网安备 11011102002149号