CF1041E Tree Reconstruction

原题链接:http://codeforces.com/contest/1041/problem/E

Tree Reconstruction

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

Monocarp has given you a list of n − 1 n−1 n1 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 ) n (2≤n≤1000) n(2n1000) — the number of vertices in the tree.

Each of the next n − 1 n−1 n1 lines contains two integers a i a_i ai and b i b_i bi each ( 1 ≤ a i &lt; b i ≤ n ) (1≤a_i&lt;b_i≤n) (1ai<bin) — the maximal indices of vertices in the components formed if the i i 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 n−1 n1 lines. Each of the last n − 1 n−1 n1 lines should contain two integers x i x_i xi and y i ( 1 ≤ x i , y i ≤ n ) y_i (1≤x_i,y_i≤n) yi(1xi,yin) — 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.

题解

每条边一定有一端的最大值为 n n n,所以我们就围着 n n n建菊花图就好了,左右两端最大值相同的边就串成一条链接在 n n n上,样例一构造如下:

1.png

样例中两个 3   4 3\ 4 3 4作为一条链, 1   4 1\ 4 1 4作为单独的一条链,然后中间的点直接贪心从大到小找没有作为端点的点作为中间的点,最后构图如下:

2.png

代码
#include<bits/stdc++.h>
using namespace std;
const int M=1005;
struct sd{int a,b;}ed[M];
int n,flag;
vector<int>cot[M];
vector<int>ans[M];
bool vis[M];
void in(){scanf("%d",&n);}
void ac()
{
	int a,b;
	for(int i=1;i<n;++i)
	{
		scanf("%d%d",&a,&b);
		if(i==1&&a==38)flag=1;
		if(a!=n&&b!=n)puts("NO"),exit(0);
		cot[a].push_back(i);cot[b].push_back(i);vis[a]=vis[b]=1;
	}
	for(int i=1;i<n;++i)
	{
		if(!cot[i].size())continue;
		ans[i].push_back(n);
		for(int j=i-1;j>=1;--j)
		{
			if(ans[i].size()==cot[i].size())break;
			if(!vis[j])ans[i].push_back(j),vis[j]=1;
		}
		if(ans[i].size()!=cot[i].size())puts("NO"),exit(0);
		ans[i].push_back(i);
	}
	puts("YES");
	for(int i=1;i<n;++i)if(ans[i].size())for(int j=0;j<cot[i].size();++j)ed[cot[i][j]]=(sd){ans[i][j],ans[i][j+1]};
	for(int i=1;i<n;++i)printf("%d %d\n",ed[i].a,ed[i].b);
}
int main(){in();ac();}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值