E. Tree Reconstruction

E. TreeReconstruction

               time limit per test

              1 second

               memory limit per test 

               256 megabytes


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.

Input

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.

Output

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.

Examples

Input

4
3 4
1 4
3 4

Output

YES
1 3
3 2
2 4

Input

3
1 3
1 3

Output

NO

Input

3
1 2
2 3

Output

NO

Note

Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs.

 

一个样例:

7

4 7

4 7

5 7

5 7

6 7

6 7

YES

。。。

AC代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n ;
struct node {
	int a;
	int b;
}g[1004],g1[1004];
bool cmp( node A, node B) {
	return A.a < B.a;
}
vector <int> v[1004];
bool vis[1024];
int main() {
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		cin >> g[i].a >> g[i].b;
	}
	sort(g, g + n - 1, cmp);
	for (int i = n-2; i >=0; i--) {
		if (g[i].a < i + 1) {
			cout << "NO";
			return 0;
		}
	}
	for (int i = 0; i < n-1; i++) {
		if (g[i].b != n) {
			cout << "NO";
			return 0;
		}
		if (v[g[i].a].size() > 0) {
			int tt = v[g[i].a][v[g[i].a].size() - 1] - 1;
			while (vis[tt]) tt--;
			vis[tt] = true;
			v[g[i].a].push_back( tt );
		}
		else {
			v[g[i].a].push_back(g[i].a);  
			vis[g[i].a] = true;  
		}
	}
	int tot = 0;
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < v[i].size(); j++) {
			g1[tot++].a = v[i][j];
			if (j == v[i].size()-1) {
				g1[tot - 1].b = n;
			}
			else {
				g1[tot - 1].b = v[i][j + 1];
			}
		}
	}
	cout << "YES" << '\n';
	for (int i = 0; i < tot; i++) {
		cout << g1[i].a << ' ' << g1[i].b << '\n';
	}
	return 0;
}
发布了46 篇原创文章 · 获赞 81 · 访问量 5549

猜你喜欢

转载自blog.csdn.net/zfq17796515982/article/details/89196367