CF1041E.Tree Reconstruction
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
莫诺卡普画出了一棵树(即一个无向、连通、无环的图),然后给每个顶点分配了一个编号。所有编号均为 1 到 n 之间互不相同的整数。对于该树的每条边 e,莫诺卡普写下了两个数:若仅删除边 e,则树被分割为两个连通分量,这两个分量中顶点编号的最大值。
莫诺卡普给你提供了一个包含 n−1 对数字的列表。他希望你构造一棵树,使得该树按上述规则生成的列表恰好等于所给列表;若这样的树存在,请给出一个例子;若不存在,请说明不存在。
输入格式
The first line contains one integer n (2≤n≤1000) — the number of vertices in the tree.
Each of the next n−1 lines contains two integers ai and bi each (1≤ai<bi≤n) — the maximal indices of vertices in the components formed if the i-th edge is removed.
第一行包含一个整数 n(2≤n≤1000)——树中顶点的数量。
接下来的 n−1 行,每行包含两个整数 ai 和 bi(1≤ai<bi≤n)——表示移除第 i 条边后所形成的两个连通分量中顶点的最大编号。
输出格式
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 xi and yi (1≤xi,yi≤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.
如果不存在能生成给定数对列表的树,则输出 "NO"(不带引号)。
否则,第一行输出 "YES"(不带引号),接下来 n−1 行输出该树的各条边。最后 n−1 行中每行应包含两个整数 xi 和 yi(1≤xi,yi≤n),表示由该边连接的两个顶点。
注意:本题中边的编号顺序无关紧要。只要您重建的树所生成的数对集合与输入文件中给出的数对集合(顺序可能不同)完全一致,您的解答即视为正确。也就是说,您可以按任意顺序输出所重构树的各条边。
输入输出样例
输入#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.

第一个示例中可能的树。虚线表示为获得合适结点对所需删除的边。

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