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。

【题解】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值