#P10309. 「Cfz Round 2」Max of Distance
「Cfz Round 2」Max of Distance
Problem Description
You are given a tree with nodes and an integer .
You need to construct an integer edge weight for each edge of the tree , such that:
- ;
- Choose a node uniformly at random. The expected value of , taken modulo , is equal to ;
or report that there is no solution.
Here, denotes the sum of edge weights on the simple path between nodes and .
If you do not know how to compute an expectation modulo , please refer to P2613 Template: Modulo of Rational Numbers.
Input Format
The first line contains an integer .
The next lines each contain two positive integers , indicating that there is an edge between nodes and in the tree .
The next line contains an integer .
Output Format
Output one line or lines:
- If there is a solution, output lines, each containing an integer , representing the edge weight of edge in your constructed tree .
- If there is no solution, output one line containing an integer .
Any output that satisfies the requirements will be accepted.
3
1 2
2 3
665496238
1
2
Hint
"Sample Explanation #1"
All values of are shown in the table below, where the red numbers are the maximum values for the node at the beginning of each row.
You can verify that $E=\dfrac{3+2+3}{3}=\dfrac{8}{3}\equiv 665496238\pmod {998244353}$.
Constraints
For all testdata, , , . It is guaranteed that the input forms a tree.
You can get the score for this problem only if you pass all test points.
Translated by ChatGPT 5