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

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. WinSCP安装与使用

      WinSCP 是一个 Windows 环境下使用的 SSH(Source Shell)的开源图形化 SFTP(SSH File Transfer Protocol) 客户端.同时支持 SCP(So ...

  2. 存储过程数据insert into select

    create or replace procedure PRO_K3_CZZH (org_name in varchar2, --财政专户名称 opertype in varchar2, --操作类型 ...

  3. 遇到的一个移动端从下往上过渡的弹框,在Android下过渡动画的优化问题。

    优化之前: /* 分享弹框样式 */ .popUpDiv { width: 100vw; height: 100vh; transition: all 0.5s ease; position: fix ...

  4. 【ssh】端口转发

    来源:https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/ 非常非常好用,可以通过跳板解决两台服务器无法连接的问题 第一部分 概述 ...

  5. java易错题----静态方法的调用

    class A{ public static String s="A.s"; } class B extends A{ public static String s="B ...

  6. postMan测试https接口

    一.如何安装postman? Postman下载地址https://www.getpostman.com/ 我下载的版本是Postman-win64-5.0.0-Setup.exe 这是免安装的,可以 ...

  7. Oracle logminer 日志挖掘

    Table of Contents 1. LOGMNR简介 2. 创建数据字典 2.1. 外部文件存储数据字典 2.2. redo log 存储数据字典 3. 添加需要分析的文件 4. 开始分析文件 ...

  8. Spring Cloud Eureka简介及原理

    Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务 ...

  9. OOP和面向对象

    OOP具有三大特点 1.封装性:也称为信息隐藏,就是将一个类的使用和实现分开,只保留部分接口和方法与外部联系,或者说只公开了一些供开发人员使用的方法.于是开发人员只 需要关注这个类如何使用,而不用去关 ...

  10. 下载离线VS2017

    1.下载工具 版本 文件 Visual Studio Enterprise (企业版) vs_enterprise.exe Visual Studio Professional (专业版) vs_pr ...