A11858 | Useful Decomposition
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!
He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!
The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.
Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.
He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!
The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.
Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.
输入格式
The first line contains a single integer $n$ ( $2 \leq n \leq 10^{5}$ ) the number of nodes in the tree.
Each of the next $n - 1$ lines contains two integers $a_i$ and $b_i$ ( $1 \leq a_i, b_i \leq n$ , $a_i \neq b_i$ ) — the edges of the tree. It is guaranteed that the given edges form a tree.
Each of the next $n - 1$ lines contains two integers $a_i$ and $b_i$ ( $1 \leq a_i, b_i \leq n$ , $a_i \neq b_i$ ) — the edges of the tree. It is guaranteed that the given edges form a tree.
输出格式
If there are no decompositions, print the only line containing "No".
Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition $m$ .
Each of the next $m$ lines should contain two integers $u_i$ , $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i \neq v_i$ ) denoting that one of the paths in the decomposition is the simple path between nodes $u_i$ and $v_i$ .
Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.
If there are multiple decompositions, print any.
Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition $m$ .
Each of the next $m$ lines should contain two integers $u_i$ , $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i \neq v_i$ ) denoting that one of the paths in the decomposition is the simple path between nodes $u_i$ and $v_i$ .
Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.
If there are multiple decompositions, print any.
输入输出样例
输入 #1
4 1 2 2 3 3 4
输出 #1
Yes 1 1 4
输入 #2
6 1 2 2 3 3 4 2 5 3 6
输出 #2
No
输入 #3
5 1 2 1 3 1 4 1 5
输出 #3
Yes 4 1 2 1 3 1 4 1 5
The tree from the first example is shown on the picture below:  The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
The tree from the second example is shown on the picture below:  We can show that there are no valid decompositions of this tree.
The tree from the third example is shown on the picture below:  The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
The tree from the second example is shown on the picture below:  We can show that there are no valid decompositions of this tree.
The tree from the third example is shown on the picture below:  The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted