原题链接: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 111 to nnn. For every edge eee of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge eee (and only this edge) is erased from the tree.

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

Each of the next n−1n−1n−1 lines contains two integers aia_iai​ and bib_ibi​ each (1≤ai&lt;bi≤n)(1≤a_i&lt;b_i≤n)(1≤ai​<bi​≤n) — the maximal indices of vertices in the components formed if the iii-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−1n−1 lines. Each of the last n−1n−1n−1 lines should contain two integers xix_ixi​ and yi(1≤xi,yi≤n)y_i (1≤x_i,y_i≤n)yi​(1≤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.

题解

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

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

代码
#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();}

CF1041E Tree Reconstruction相关推荐

  1. UVa10410 Tree Reconstruction(bfs+dfs确定二叉树)

    题意就是给你二叉树的bfs和dfs遍历,都是结点小的优先遍历 本来自己没做出来,不想写的,做了半天测试数据是对的,但是WA.因为看了一个大佬的解法,让我感慨万分,简洁而直接的解法,正确而清晰的思路,让 ...

  2. Tree Reconstruction UVA - 10410

    题目大意:给你一棵树的层次遍历和前序遍历,让你复原这棵树 思路:这是分类讨论的题,感觉挺难的 此题的关键,是要把已知的两次遍历配合起来. 考虑前序遍历中的前后两个点,分别记作A和B,那B点可能是A点的 ...

  3. UVA 10410——Tree Reconstruction

    题意:给定一颗树的BFS和DFS,求这棵的每个节点. 思路:用栈模拟维护.对应的BFS为每个节点到根节点的距离,然后比较当前节点和栈顶节点与根的距离,如果当前节点大,则为栈顶节点的孩子,否则弹出继续比 ...

  4. CF1041E Tree Reconstruction_构造_思维题

    不难发现,每次询问结果一定是 (i,n)(i,n)(i,n), 而 iii 出现的次数恰好是 iii 到 i′i'i′ 的距离(i′i'i′ 是第一个不与 iii 相等的数).我们可以将这颗树构造成一 ...

  5. 重测序群体遗传进化分析之进化树构建

    tree 重测序大家都不陌生,它是检测样本基因组变异(SNP,indel,SV,CNV)的主要手段之一,有了这些变异信息,后续可以做很多分析工作,例如: 遗传群体可以进行遗传图谱构建.BSA分析等:大 ...

  6. linux 建树软件,新一代建树工具IQ-Tree介绍

    系统发育树(phylogenetic tree)构建的软件可谓五花八门.大体上,有如下几种策略:从最简单的UPGMA法,到邻接法(neighbor joining),最大简约法(most parsim ...

  7. 《算法竞赛入门经典(第2版)》——学习记录

    前言:   这里主要记录本人在学习紫书过程中充分理解过的题目的AC代码,便于以后回顾时查找代码和思路,毕竟看别人的真的有点难懂.此外,本书甚至是本书之外的相关知识学习也可能在此留下记录.   作为一只 ...

  8. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  9. (二十:2020.11.06)MICCAI 2020 追踪之论文纲要(译)<下>

    讲在前面 此文章包含第五.第六和第七部分. 论文目录 PART V <Biological, Optical and Microscopic Image Analysis 生物,光学和显微图像分 ...

  10. 战斗在生物信息学前沿的科学“游击战士”—— 纪念郝柏林院士

    本文转载自GPB官方公众号(GPBees),已获授权 著名物理学家和生物信息学家.中国科学院院士.原理论物理研究所所长(任期:1990 - 1994)郝柏林教授于2018年3月7日去世,享年83岁.鉴 ...

最新文章

  1. Placements(连接)
  2. JavaScript正则表达式
  3. NPS cisco 802.1x window7 实现认证接入网络
  4. 恍然小悟,去掉Excel的worksheet(工作表)保护的新方法
  5. 上传html 0字节,HTML ajax 上传文件限制文件的类型和文件大小
  6. 高考英语口试计算机系,大学英语口语考试都考什么,高考英语口试考什么?
  7. 《CLR via C#》 第三版的主要内容
  8. sqlca.sqlcode!=0
  9. 这下终于可以方便地发表测试型网页代码了(附源码下载)
  10. 编写shell脚本实现tomcat定时重启的方法
  11. php调用winhttp,HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)
  12. FastStone Capture:Windows系统下小巧好用的宝藏电脑截图软件
  13. 寻找隐藏在CloudFlare和Tor后的真实IP
  14. 985学生:为什么现在学校还在教C语言?| 文末送书
  15. 如何编写GIMP插件(三)
  16. 勾股定理的毕达哥拉斯证明
  17. CPU卡FM1208发卡操作流程(不带密钥验证)
  18. 【LeetCode】买卖股票的最佳时机含手续费 [M](动态规划)
  19. eclipse安装hadoop插件教程
  20. 程序员苹果电脑使用入门

热门文章

  1. 怎么配置php发送邮件环境,如何配置PHP发送电子邮件?
  2. Raki的图论入门到进阶算法简单总结
  3. jq 获取父元素html,jq获取父级元素_使用jquery获取父元素或父节点的方法
  4. 陕西省单招计算机应用考什么,陕西省对口单招计算机应用基础模拟试题四
  5. The Luckiest Number 欧拉函数
  6. 深入理解二阶段提交协议(DDB对XA悬挂事务的处理分析)(一)
  7. java 判断两个时间相差的天数
  8. TCPDUMP 用法(转)
  9. 最新BIOS设置中英文对照表
  10. mysql truncate耗时_线上MySQL 32G大小的表truncate所消耗的时间为2分钟,快还是慢?...