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

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. web的分页方法

    web分页的三种方式,闲来无事总结一下. 1.使用前端表格插件进行分页 例如用bootstrap的拓展table组件,注意设置其分页属性时设置为"client", 即是 sideP ...

  2. 前端javascript

    前端 JavaScript   javaScript----数据库jquery $(function(){ 执行代码   });  基本语法:$(selector).action() $(" ...

  3. C++标准库和标准模板库(转)

    转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...

  4. tomcat 报错处理

    一.tomcat报错找不到资源集市 原因:tomcat的配置文件sever.xml 里的 docbase配置被Eclispe修改了 解决方法:修改回来 <Context docBase=&quo ...

  5. jsp 错误处理

    JSP提供了很好的错误处理能力.除了在Java代码 中可以使用try语句,还可以指定一个特殊页面.当应 用页面遇到未捕获的异常时,用户将看到一个精心设计 的网页解释发生了什么,而不是一个用户无法理解的 ...

  6. CF1000G

    蜜汁树形dp... 首先分析一下:他要求一条边至多只能经过两次,那么很容易会发现:从x到y这一条路径上的所有边都只会被经过一次.(如果过去再回来那么还要过去,这样就三次了,显然不合法) 那么其他能产生 ...

  7. C语言访问一个链接

    示例代码1: # include <Windows.h> int main(){ system("start http://""www.baidu.com&q ...

  8. Jmeter 谷歌插件工具blazemeter录制脚本

    1.下载谷歌浏览器插件工具:blazemeter. 2.在谷歌浏览器中拖放安装扩展工具:blazemeter. 粘贴的图像828x219 13.5 KB 3.测试网站利用这个工具录制jmter脚本. ...

  9. 滴水穿石-10GUI

    GUI 图形用户界面 1 Frame 窗体 package d10; //第一导入包 import java.awt.Frame; import java.awt.event.WindowAdapte ...

  10. 关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法

    关于金蝶k3 wise供应生门户登陆界面屏蔽业务账套多余功能模块设置方法 1. 找到以下路径 ...\Kingdee\K3ERP\KDHR\SITEFILE\WEBUI\ 找到“Login.aspx” ...