CF1041E Tree Reconstruction

版权声明:大佬您能赏脸,蒟蒻倍感荣幸,还请联系我让我好好膜拜。 https://blog.csdn.net/ShadyPi/article/details/82762533

原题链接: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 to n n . For every edge 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 (and only this edge) is erased from the tree.

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

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

扫描二维码关注公众号,回复: 3416779 查看本文章
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 上,样例一构造如下:

1.png

样例中两个 3   4 3\ 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();}

猜你喜欢

转载自blog.csdn.net/ShadyPi/article/details/82762533