A12708 | Add on a Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with $n$ nodes. In the beginning, $0$ is written on all edges. In one operation, you can choose any $2$ distinct leaves $u$ , $v$ and any real number $x$ and add $x$ to values written on all edges on the simple path between $u$ and $v$ .
For example, on the picture below you can see the result of applying two operations to the graph: adding $2$ on the path from $7$ to $6$ , and then adding $-0.5$ on the path from $4$ to $5$ .
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree $1$ . Simple path is a path that doesn't contain any node twice.
You are given a tree with $n$ nodes. In the beginning, $0$ is written on all edges. In one operation, you can choose any $2$ distinct leaves $u$ , $v$ and any real number $x$ and add $x$ to values written on all edges on the simple path between $u$ and $v$ .
For example, on the picture below you can see the result of applying two operations to the graph: adding $2$ on the path from $7$ to $6$ , and then adding $-0.5$ on the path from $4$ to $5$ .
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree $1$ . Simple path is a path that doesn't contain any node twice.
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 10^5$ ) — the number of nodes.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ , $u \neq v$ ), meaning that there is an edge between nodes $u$ and $v$ . It is guaranteed that these edges form a tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ , $u \neq v$ ), meaning that there is an edge between nodes $u$ and $v$ . It is guaranteed that these edges form a tree.
输出格式
If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
Otherwise, output "YES".
You can print each letter in any case (upper or lower).
输入输出样例
输入 #1
2 1 2
输出 #1
YES
输入 #2
3 1 2 2 3
输出 #2
NO
输入 #3
5 1 2 1 3 1 4 2 5
输出 #3
NO
输入 #4
6 1 2 1 3 1 4 2 5 2 6
输出 #4
YES
In the first example, we can add any real $x$ to the value written on the only edge $(1, 2)$ .
In the second example, one of configurations that we can't reach is $0$ written on $(1, 2)$ and $1$ written on $(2, 3)$ .
Below you can see graphs from examples $3$ , $4$ :
 
In the second example, one of configurations that we can't reach is $0$ written on $(1, 2)$ and $1$ written on $(2, 3)$ .
Below you can see graphs from examples $3$ , $4$ :
 
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted