CF1844G.Tree Weights

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn nodes labelled 1,2,…,n1,2,\dots,n. The ii-th edge connects nodes uiu_i and viv_i and has an unknown positive integer weight wiw_i. To help you figure out these weights, you are also given the distance did_i between the nodes ii and i+1i+1 for all 1≤i≤n−11 \le i \le n-1 (the sum of the weights of the edges on the simple path between the nodes ii and i+1i+1 in the tree).

Find the weight of each edge. If there are multiple solutions, print any of them. If there are no weights wiw_i consistent with the information, print a single integer −1-1.

你被给定一棵包含 nn 个节点的树,节点编号为 1,2,…,n1,2,\dots,n。第 ii 条边连接节点 uiu_i 和 viv_i,其权重 wiw_i 是一个未知的正整数。为了帮助你确定这些权重,你还被给定所有 1≤i≤n−11 \le i \le n-1 对应的节点 ii 与 i+1i+1 之间的距离 did_i(即树中节点 ii 与 i+1i+1 之间唯一简单路径上所有边的权重之和)。

请确定每条边的权重。若存在多个解,输出任意一个即可;若不存在满足条件的权重 wiw_i,则仅输出一个整数 −1-1。

输入格式

The first line contains a single integer nn (2≤n≤1052 \le n \le 10^5).

The ii-th of the next n−1n-1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i,v_i \le n, ui≠viu_i \ne v_i).

The last line contains n−1n-1 integers d1,…,dn−1d_1,\dots,d_{n-1} (1≤di≤10121 \le d_i \le 10^{12}).

It is guaranteed that the given edges form a tree.

第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)。

接下来的 n−1n-1 行中,第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i,v_i \le n,且 ui≠viu_i \ne v_i)。

最后一行包含 n−1n-1 个整数 d1,…,dn−1d_1,\dots,d_{n-1}(1≤di≤10121 \le d_i \le 10^{12})。

保证所给的边构成一棵树。

输出格式

If there is no solution, print a single integer −1-1. Otherwise, output n−1n-1 lines containing the weights w1,…,wn−1w_1,\dots,w_{n-1}.

If there are multiple solutions, print any of them.

如果无解,输出一个整数 −1-1。否则,输出 n−1n-1 行,包含权重 w1,…,wn−1w_1,\dots,w_{n-1}。

若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    5
    1 2
    1 3
    2 4
    2 5
    31 41 59 26

    输出#1

    31
    10
    18
    8
  • 输入#2

    3
    1 2
    1 3
    18 18

    输出#2

    -1
  • 输入#3

    9
    3 1
    4 1
    5 9
    2 6
    5 3
    5 8
    9 7
    9 2
    236 205 72 125 178 216 214 117

    输出#3

    31
    41
    59
    26
    53
    58
    97
    93

说明/提示

In the first sample, the tree is as follows:

In the second sample, note that w2w_2 is not allowed to be 00 because it must be a positive integer, so there is no solution.

In the third sample, the tree is as follows:

在第一个样例中,树的结构如下:

在第二个样例中,注意 w2w_2 不能为 00,因为它必须是一个正整数,因此无解。

在第三个样例中,树的结构如下:

输入解题思路,AI测评打分。不知道怎么写?

首页