Tree Reconstruction Gym - 101911G(构造)
---恢复内容开始---
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
4
3 4
1 4
3 4
YES
1 3
3 2
2 4
3
1 3
1 3
NO
3
1 2
2 3
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(构造)的更多相关文章
- 【构造题 贪心】cf1041E. Tree Reconstruction
比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...
- Aizu - 2564 Tree Reconstruction 并查集
Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...
- E. Tree Reconstruction 解析(思維)
Codeforce 1041 E. Tree Reconstruction 解析(思維) 今天我們來看看CF1041E 題目連結 題目 略,請直接看原題 前言 一開始完全搞錯題目意思,還以為每次會刪除 ...
- codeforces 1041 E. Tree Reconstruction 和度数有关的构造树
CF 1041E:http://codeforces.com/contest/1041/problem/E 题意: 告诉你一个树的节点个数,显然有n-1条边.已知去掉一条边后,两个集合中最大的节点值. ...
- Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)
题目链接:http://codeforces.com/contest/1041/problem/E 题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b . 题 ...
- 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 ...
- 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 ...
- BZOJ.4319.[cerc2008]Suffix reconstruction(后缀数组 构造 贪心)
题目链接 \(Description\) 给定SA数组,求满足SA[]的一个原字符串(每个字符为小写字母),无解输出-1. \(Solution\) 假设我们现在有suf(SA[j]),要构造suf( ...
- [CF1041E]Tree Reconstruction
题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...
随机推荐
- Java学习——面向对象【3】
1. 继承 java的类是单继承(一个子类只能继承一个父类),不能多继承(多个父类):A继承自B,A也继承自C,可以多重继承,就是A继承自B,B继承自C(A->B->C), 所有的类都继 ...
- STM32L476应用开发之五:数据保存与SD卡操作
便携式气体分析仪的特点就是离线运行.尽管是离线运行,但测试数据还是需要的,所以采取方式保存数据就是必须的.在本次项目中我们计划采用SD卡来保存数据. 1.硬件设计 该读卡器整合 SD 卡规范和 FAT ...
- 用来表达更复杂的sql语句!!!!!extra 原声sql
extra 用来表达更复杂的sql语句!!!!! extra可以指定一个或多个 参数,例如 select, where or tables. 这些参数都不是必须的,但是你至少要使用一个!要注意这些额外 ...
- AI学习吧-登录注册
登录注册注销 如果需要给表设置权限,没有登录就不可以查看,只需要在每个视图函数之前加上Auth_classes=[ ]即可! 增加两张表,做登录认证 #models.py #做登录验证 class U ...
- Python_网络编程udp-飞秋自动攻击
# 模拟一个接收数据import socketimport time def auto_hack(udp_socket, recv_msg, revc_ip, revc_port=2425): # 发 ...
- python 作用域
什么是命名空间 == 对一个名字起作用的范围 # def test():# print("----test----") # import test# test.test() # f ...
- MVC AOP解决JsonResult返回json时间格式
新建JsonNetResult类:JsonResult public class JsonNetResult: JsonResult { public JsonNetResult() { Settin ...
- IIS中配置访问HTTPS
1,新建网站,选中类型为 https,然后更改SSL证书为你配置的SSL证书, 对于SSL证书的配置是这样的 点开第二步,然后点击 创建自签名证书 确定以后点开网站看到有个SSL, 双击进去,再选中 ...
- JSP基础知识➣获取参数和过滤器(四)
JSP表单提交和参数获取 JSP表单提交的两种方式:post和get,通过这两种方式提交的参数到后台,获取参数的值主要由request来处理,获取值的方式有以下几种: getParameter(): ...
- 公共语言运行时支持(/clr)
项目属性 -> 配置属性 -> “常规”里开启“公共语言运行时支持(/clr)