#3294. UVA 10410Tree Reconstruction
UVA 10410Tree Reconstruction
Tree Reconstruction
题面翻译
题目大意:
有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在
输入格式:
第一行一个整数
往下行,每行两个整数,表示去掉这条边后两个联通块中点的最大的编号是多少
输出格式:
如果不存在满足条件的树,输出"NO"
否则输出"YES",下面行每行描述一条树边(两端点的编号)
题目描述
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from to . For every edge of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge (and only this edge) is erased from the tree.
Monocarp has given you a list of 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 ( ) — the number of vertices in the tree.
Each of the next lines contains two integers and each ( ) — the maximal indices of vertices in the components formed if the -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 lines. Each of the last lines should contain two integers and ( ) — 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
样例输入 #1
4
3 4
1 4
3 4
样例输出 #1
YES
1 3
3 2
2 4
样例 #2
样例输入 #2
3
1 3
1 3
样例输出 #2
NO
样例 #3
样例输入 #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.