---恢复内容开始---

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 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 xixi and 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

Note

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

题意:就是给你n-1对点,每对点代表的是在树中隔断一条边所形成的两个不连通子树中的最大的节点(例如:隔断4-2这条边,所形成的点对为3、4),问你是否可以构造处一棵树,使其满足所给点对的要求,并输出这棵树的边!思路:我们知道不管割去哪一条边,两点对中一点是会出现n的,因为变成两个子树,n肯定会在其中一棵树中,并且大于这棵树其他节点。①给出的节点对必须包含n既然,每点对都包含n,那么我们很容易想到让n成为根节点,每次出现一个新节点,就建立一条新分支,这样就满足割去该边,得到对应点

但是,还有一组3、4点对,从图中知道,这应该是2节点产生的,也就是我们节点2处于3、4节点所在子树任意一个,并且为了能显示处点对3、4,这个未使用的节点2应该小于点对3、4中任意一个

②插入的节点应该小于点对中任意一个节点

也就是说,对于出现过一次的点对,我们直接将其之间建立边;出现多次的,我们就在该点对中间插入未出现的节点;

怎么插才能保证正确性呢?所以我们选择从大到小寻找出现多次的点对,然后再该点对中也是尽量插入大的未出现的节点,也就是将大的节点尽量查到大的点对之间,如果这样还无法插入,说明无法构造这样的树。

#include<bits/stdc++.h>
using namespace std; int n;
int vis[];
int s[];
int ll[];
int rr[];
int main()
{
scanf("%d",&n);
int flag = ;
for(int i=; i<n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a != n && b != n)
{
flag = ;
}
else if(a != n)
vis[a]++;
else
vis[b]++;
}
int top = ;
int cnt = ;
if(!flag)
{
for(int i=; i<n; i++)
{
if(!vis[i])
{
s[++top]=i;
}
}
for(int i=n-;i;i--)
{
if(!vis[i])continue;
vis[i]--;
int u = n,v;
while(vis[i]--)
{
v = s[top--];
if(v > i)
{
flag = ;
break;
}
ll[++cnt] = u;
rr[cnt] = v;
u = v;
}
if(flag)break;
ll[++cnt] = u;
rr[cnt] = i;
}
}
if(flag)printf(" NO\n");
else
{
printf("YES\n");
for(int i=;i<=cnt;i++)
{
printf("%d %d\n",ll[i],rr[i]);
}
}
}

Tree Reconstruction Gym - 101911G(构造)的更多相关文章

  1. 【构造题 贪心】cf1041E. Tree Reconstruction

    比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...

  2. Aizu - 2564 Tree Reconstruction 并查集

    Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...

  3. E. Tree Reconstruction 解析(思維)

    Codeforce 1041 E. Tree Reconstruction 解析(思維) 今天我們來看看CF1041E 題目連結 題目 略,請直接看原題 前言 一開始完全搞錯題目意思,還以為每次會刪除 ...

  4. codeforces 1041 E. Tree Reconstruction 和度数有关的构造树

    CF 1041E:http://codeforces.com/contest/1041/problem/E 题意: 告诉你一个树的节点个数,显然有n-1条边.已知去掉一条边后,两个集合中最大的节点值. ...

  5. Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)

    题目链接:http://codeforces.com/contest/1041/problem/E 题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b . 题 ...

  6. CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)

    Misha walked through the snowy forest and he was so fascinated by the trees to decide to draw his ow ...

  7. Tree Restoration Gym - 101755F (并查集)

    There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...

  8. BZOJ.4319.[cerc2008]Suffix reconstruction(后缀数组 构造 贪心)

    题目链接 \(Description\) 给定SA数组,求满足SA[]的一个原字符串(每个字符为小写字母),无解输出-1. \(Solution\) 假设我们现在有suf(SA[j]),要构造suf( ...

  9. [CF1041E]Tree Reconstruction

    题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...

随机推荐

  1. android招聘啦,美图秀秀欢迎你加入!

    前言 最近朋友公司招聘,美图秀秀大家一定很熟悉吧,欢迎你的加入. 了解相关更多技术以外的,可参考<除了敲代码,你还有什么副业吗?>,再往下看,今天给需要换工作或者还未找到工作的童鞋们谋一个 ...

  2. 不能够连接到主机(名称为localhost)上的MySQL服务”

    不能够连接到主机(名称为localhost)上的MySQL服务” -如果是服务未启动.那么就右键‘计算机’---->管理--->服务和应用程序---->服务,在右侧的栏目中找到名称为 ...

  3. Oracle12c安装和卸载图文教程

    注:本文来源于:<Oracle12c安装和卸载图文教程> 一.安装 1.去官网下载相应的版本 2.下载好的两个压缩文件压缩到一个文件夹中 3.打开上个步骤的文件夹,运行stepup,显示如 ...

  4. 自定义你的 Confluence 6 站点

    本部分对 Confluence 全站进行自定义的相关内容进行了说明.这部分只能是具有 Confluence 的管理员权限的用户才能进行操作 —— 系统管理员或者 Confluence 管理员. 有关对 ...

  5. Java的动手动脑(五)

    日期:2018.11.1 星期四 博客期:021 Part1: 运行代码 class Grandparent { public Grandparent() { System.out.println(& ...

  6. js 判断输入的内容是否是整数

    需求简介:列表有一列排序,其值只能是整数,不能是小数,在js中判断,并给出提示 解决思路:在js中先获取表单的值,然后用isNaN,然后查一下怎么把小数排除在外.我靠( ‵o′)凸,这只能算是半路把! ...

  7. Android相关 博客收藏

    #1 Android 网络编程 参考博客 :http://blog.csdn.net/kieven2008/article/details/8210737 #2 Could not find com. ...

  8. 异常:Keyword not supported: 'data source'的解决办法

    将连接字符串中的&quot换为“'”,一个单引号即可. 详细解释:https://blogs.msdn.microsoft.com/rickandy/2008/12/09/explicit-c ...

  9. HTTP协议请求头信息和响应头信息

    阅读目录 http的请求部分 常用请头信息 常用响应头信息 http的请求部分 基本结构 请求行 GET  /test/hello.html HTTP/1.1 消息头(并不是每一次请求都一样) 空行 ...

  10. sqoop的基本语法详解及可能遇到的错误

    1 sqoop介绍 Apache Sqoop是专为Apache Hadoop和结构化数据存储如关系数据库之间的数据转换工具的有效工具.你可以使用Sqoop从外部结构化数据存储的数据导入到Hadoop分 ...