其他分享
首页 > 其他分享> > Codeforces Round #756 (Div. 3) D题

Codeforces Round #756 (Div. 3) D题

作者:互联网

D. Weights Assignment For Tree Edges

比赛将近一个小时都花在这题上然而最后没出来,后来又花了很长时间实现,后来才知道没理解题意;

1.先给定的第一个数组是存第i个节点的根
2.第二个数组p是顶点v的排序,是根到该节点v权值w的排序,形成固定的序列p

If it is possible, assign any positive integer weights on the edges, so that the vertices sorted by distance from the root would form the given permutation p.

If the solution exists, print an array of n integers w1,w2,…,wn, where wi is the weight of the edge that leads from bi to i. For the root there is no such edge, so use the value wi=0. For all other vertices, the values of wi must satisfy the inequality 1≤wi≤109. There can be equal numbers among wi values, but all sums of weights of edges from the root to vertices must be different and satisfy the given permutation.
If there are several solutions, output any of them.
If no solution exists, output -1.

wi就是根减去值:wi=root[i]-i ;

注意:wi的范围 ( 0<=wi<=109),不能满足就是输出-1的情况,当wi<0,输出-1。

【题解】

标签:756,exists,solution,vertices,wi,Codeforces,output,Div,root
来源: https://blog.csdn.net/m0_51506743/article/details/121597191