A11971 | Tree Reconstruction
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$ . For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
输入格式
The first line contains one integer $n$ ( $2 \le n \le 1\,000$ ) — the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ( $1 \le a_i < b_i \le n$ ) — the maximal indices of vertices in the components formed if the $i$ -th edge is removed.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ( $1 \le a_i < b_i \le n$ ) — the maximal indices of vertices in the components formed if the $i$ -th edge is removed.
输出格式
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ ) — vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ ) — vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
输入输出样例
输入 #1
4 3 4 1 4 3 4
输出 #1
YES 1 3 3 2 2 4
输入 #2
3 1 3 1 3
输出 #2
NO
输入 #3
3 1 2 2 3
输出 #3
NO
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs.


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