#P10309. Tree Edge Weight Assignment
Tree Edge Weight Assignment
Tree Edge Weight Assignment
You are given a tree with nodes and an integer . Your task is to assign an integer weight to each edge of the tree such that:
- for every edge.
- Define as the sum of the weights along the unique simple path between nodes and . For a node , let [ M(u)= \max_{v=1}^{n} dis(u,v), ] and consider a node chosen uniformly at random from to . The expected value of , taken modulo , must be exactly .
If there is no valid assignment of weights that satisfies these conditions, output -1.
If you are not familiar with computing expectations and working with modular arithmetic in this setting, please refer to the template problem P2613 \textbf{Template: Rational Numbers Modulo} for guidance.
inputFormat
The first line contains two integers and ( and ). The following lines each contain two integers and describing an edge between nodes and in the tree.
outputFormat
If there exists an assignment of weights satisfying the conditions, output space‐separated integers representing the weight for each edge in the order of input. Otherwise, output -1.
Note: In a tree with one node (), since there are no edges, output an empty line if , otherwise output -1.
sample
1 0