QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 512 MB 満点: 100

#16532. Max of Distance

統計

Given a tree $G$ with $n$ nodes and an integer $E$.

You need to assign an integer weight $w_i$ to each edge in the tree $G$ such that:

  • $1 \le w_i \le 10^9$;
  • The expected value of $\max\limits_{v=1}^n\operatorname{dis}(u,v)$ when a node $u$ is chosen uniformly at random, modulo $998244353$, is equal to $E$;

Or report that no such assignment exists.

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

Input

The first line contains an integer $n$.

The next $n-1$ lines each contain two positive integers $u_i, v_i$, representing an edge $(u_i, v_i)$ in the tree $G$.

The next line contains an integer $E$.

Output

Output either one line or $n-1$ lines:

  • If a solution exists, output $n-1$ lines, each containing an integer $w_i$, representing the weight of the edge $(u_i, v_i)$ in your constructed tree $G$.
  • If no solution exists, output a single line containing the integer $-1$.

Any valid output is acceptable.

Examples

Input 1

3
1 2
2 3
665496238

Output 1

1
2

Note 1

All $\operatorname{dis}$ values are shown in the table below, where the maximum $\operatorname{dis}$ for each starting node is highlighted in red.

$\operatorname{dis}$ $1$ $2$ $3$
$1$ $0$ $1$ $\color{red}3$
$2$ $1$ $0$ $\color{red}2$
$3$ $\color{red}3$ $2$ $0$

It can be verified that $E=\dfrac{3+2+3}{3}=\dfrac{8}{3}\equiv 665496238\pmod {998244353}$.

Constraints

For all test cases, $2\le n\le 10^5$, $1 \le u_i,v_i \le n$, $0\le E < 998244353$, and the input is guaranteed to form a tree.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.