A16000 | Tree Weights
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree with $n$ nodes labelled $1,2,\dots,n$ . The $i$ -th edge connects nodes $u_i$ and $v_i$ and has an unknown positive integer weight $w_i$ . To help you figure out these weights, you are also given the distance $d_i$ between the nodes $i$ and $i+1$ for all $1 \le i \le n-1$ (the sum of the weights of the edges on the simple path between the nodes $i$ and $i+1$ in the tree).
Find the weight of each edge. If there are multiple solutions, print any of them. If there are no weights $w_i$ consistent with the information, print a single integer $-1$ .
Find the weight of each edge. If there are multiple solutions, print any of them. If there are no weights $w_i$ consistent with the information, print a single integer $-1$ .
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 10^5$ ).
The $i$ -th of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ( $1 \le u_i,v_i \le n$ , $u_i \ne v_i$ ).
The last line contains $n-1$ integers $d_1,\dots,d_{n-1}$ ( $1 \le d_i \le 10^{12}$ ).
It is guaranteed that the given edges form a tree.
The $i$ -th of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ( $1 \le u_i,v_i \le n$ , $u_i \ne v_i$ ).
The last line contains $n-1$ integers $d_1,\dots,d_{n-1}$ ( $1 \le d_i \le 10^{12}$ ).
It is guaranteed that the given edges form a tree.
输出格式
If there is no solution, print a single integer $-1$ . Otherwise, output $n-1$ lines containing the weights $w_1,\dots,w_{n-1}$ .
If there are multiple solutions, print any of them.
If there are multiple solutions, print any of them.
输入输出样例
输入 #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 $w_2$ is not allowed to be $0$ because it must be a positive integer, so there is no solution.
In the third sample, the tree is as follows:

In the second sample, note that $w_2$ is not allowed to be $0$ because it must be a positive integer, so there is no solution.
In the third sample, the tree is as follows:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted