Gym - 101911G Tree Reconstruction

22 篇文章 0 订阅
8 篇文章 0 订阅

Statements

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 eeof 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 n−1n−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 nn (2≤n≤10002≤n≤1000) — the number of vertices in the tree.

Each of the next n−1n−1 lines contains two integers aiai and bibi each (1≤ai<bi≤n1≤ai<bi≤n) — the maximal indices of vertices in the components formed if the ii-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−1n−1 lines. Each of the last n−1n−1 lines should contain two integers xixiand yiyi (1≤xi,yi≤n1≤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

题意:n个点,给出n-1条边  每条边的左右部分的 最大节点编号  是否能还原这个树

题解:首先给出的两点一定要有一个是节点n,否则一定不符合

然后我们保存一下较小的那个节点,排序,从小到大开始连接,若遇见一样的,那就看是否有更小的节点没有连接,这样我们构造一个一条线的树就可以了,详见代码

#include<bits/stdc++.h>
using namespace std;
const int N=1100;
set<int> s;
int n,a[N],ans[N];
int main()
{
	int x,y;
	int flag=1;
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		if(max(x,y)!=n) flag=0; // 判断每对 是否有个节点 n 
		a[i]=min(x,y);
		s.insert(i);
	}
	if(!flag)
	{
		printf("NO\n");
		return 0;
	}
	sort(a+1,a+n);  // 把较小的节点 排序 
	
	s.insert(n);
	
	ans[1]=a[1];
	s.erase(a[1]);
	
	for(int i=2;i<n;i++)
	{
		if(a[i]==a[i-1]) // 若遇见一样的  
		{
			int point=*s.begin(); // 判断是否有小的点 没有连接 
			if(point<a[i])
			{
				ans[i]=point;
				s.erase(point);
			}
			else
			{
				printf("NO\n");
				return 0;
			}
		}
		else
		{
			ans[i]=a[i];
			s.erase(a[i]);
		}
	}
	ans[n]=n;
	printf("YES\n");
	for(int i=1;i<n;i++) printf("%d %d\n",ans[i],ans[i+1]);
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值