CF1041E.Tree Reconstruction

普及/提高-

通过率:0%

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 11 to nn . For every edge ee of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge ee (and only this edge) is erased from the tree.

Monocarp has given you a list of n1n - 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 nn ( 2n10002 \le n \le 1\,000 ) — the number of vertices in the tree.

Each of the next n1n-1 lines contains two integers aia_i and bib_i each ( 1ai<bin1 \le a_i < b_i \le n ) — the maximal indices of vertices in the components formed if the ii -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 n1n - 1 lines. Each of the last n1n - 1 lines should contain two integers xix_i and yiy_i ( 1xi,yin1 \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.

首页