#P10309. 「Cfz Round 2」Max of Distance

    ID: 11262 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学洛谷原创Special JudgeO2优化树的直径树论构造洛谷月赛

「Cfz Round 2」Max of Distance

Problem Description

You are given a tree GG with nn nodes and an integer EE.

You need to construct an integer edge weight wiw_i for each edge of the tree GG, such that:

  • 1wi1091 \le w_i \le 10^9;
  • Choose a node uu uniformly at random. The expected value of maxv=1ndis(u,v)\max\limits_{v=1}^n\operatorname{dis}(u,v), taken modulo 998244353998244353, is equal to EE;

or report that there is no solution.

Here, dis(u,v)\operatorname{dis}(u,v) denotes the sum of edge weights on the simple path between nodes uu and vv.

If you do not know how to compute an expectation modulo 998244353998244353, please refer to P2613 Template: Modulo of Rational Numbers.

Input Format

The first line contains an integer nn.

The next n1n-1 lines each contain two positive integers ui,viu_i,v_i, indicating that there is an edge (ui,vi)(u_i,v_i) between nodes uiu_i and viv_i in the tree GG.

The next line contains an integer EE.

Output Format

Output one line or n1n-1 lines:

  • If there is a solution, output n1n-1 lines, each containing an integer wiw_i, representing the edge weight of edge (ui,vi)(u_i,v_i) in your constructed tree GG.
  • If there is no solution, output one line containing an integer 1-1.

Any output that satisfies the requirements will be accepted.

3
1 2
2 3
665496238
1
2

Hint

"Sample Explanation #1"

All values of dis\operatorname{dis} are shown in the table below, where the red numbers are the maximum dis\operatorname{dis} values for the node at the beginning of each row.

dis\operatorname{dis} 11 22 33
11 00 11 3\color{red}3
22 11 00 2\color{red}2
33 3\color{red}3 22 00

You can verify that $E=\dfrac{3+2+3}{3}=\dfrac{8}{3}\equiv 665496238\pmod {998244353}$.

Constraints

For all testdata, 2n1052\le n\le 10^5, 1ui,vin1 \le u_i,v_i \le n, 0E<9982443530\le E < 998244353. 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