题目链接:http://codeforces.com/contest/1041/problem/E

题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b 。

题解:显示最大值出现的次数为n - 1,且i点出现的次数小于等于i。一个数字 i(< n)出现的次数为 i 到 n 的距离,可构造出一条链使得满足条件。

 #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LL __int128
#define ull unsigned long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
#define pi acos(-1)
#define pii pair<int,int>
#define pb push_back
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const int MAXN = 1e3 + ;
const int MAXM = 2e6 + ;
const ll mod = 1e9 + ; int du[MAXN]; int main()
{
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int n;
scanf("%d",&n);
bool flag = true;
for(int i = ; i < n; i++) {
int a,b;
scanf("%d%d",&a,&b);
du[a]++;
if(b != n) flag = false;
}
int now = ;
for(int i = ; i < n; i++) {
now += du[i];
if(now > i) flag = false;
}
if(!flag) {
puts("NO");
return ;
}
puts("YES");
set<int>se;
for(int i = ; i <= n; i++) se.insert(i);
int pre = -;
for(int i = ; i < n; i++) {
if(du[i]) {
if(pre != -) printf("%d %d\n",pre,i);
pre = i;
du[i]--, se.erase(i);
}
while(du[i]) {
printf("%d %d\n",pre,*se.begin());
pre = *se.begin();
du[i]--, se.erase(se.begin());
}
}
printf("%d %d\n",pre,n);
return ;
}

Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)的更多相关文章

  1. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  2. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  3. Codeforces Round #509 (Div. 2)

    咕咕咕了好多天终于有时间写篇博客了_(:з」∠)_ 打网赛打到自闭的一周,终于靠这场CF找回了一点信心... 1041A - Heist \(ans=max\left \{ a_i \right \} ...

  4. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  5. Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  6. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  8. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  9. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

随机推荐

  1. POJ 2195 Going Home 【最小费用最大流】

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:2715 ...

  2. 拯救你丢失的精度——BigInteger和BigDecimal类(入门)

    第三阶段 JAVA常见对象的学习 BigInteger和BigDecimal类 BigInteger类 (一) 构造方法: //针对超过整数范围的运算(整数最大值:2147483647) BigInt ...

  3. Kubernetes组件-DaemonSet

    ⒈简介 Replicationcontroller和ReplicaSet都用于在Kubermetes集群上部署运行特定数量的pod.但是,当某些情况下我们希望在集群中的每个节点上运行同一个指定的pod ...

  4. macos catalina安装python3

    之前跟着教程用brow安装了python3,后来发现电脑上有三个版本的python,头大. 于是用brew uninstall --force python3卸掉了python3 看到现在有两个版本 ...

  5. python 入门(基础)

    1. python的常见数据类型(str , list ,dict,set) str (字符串的操作方法) astr = " Hello Workd " astr.strip() ...

  6. python-day5(正式学习)

    格式化输出 符合某种输出规范的print函数的应用 第一种方式 使用占位符.漫威里有个人叫斯塔克,他平时站在人堆里(print函数引号内的内容)我们看不出来和其他人有什么异常(print的终端显示), ...

  7. Asp.net core 学习笔记 ef core Surrogate Key, Natural Key, Alternate Keys

    更新: 2019-12-23 foreignkey 并不一样要配上 alternate key,其实只要是 unique 就可以了. 和 sql server 是一样的, 经常有一种错觉 primar ...

  8. Redis 消息队列 初体验

    队列之生产者.消费者模式 using System; using System.Threading; using NServiceKit.Redis; namespace ConsoleApplica ...

  9. 高性能网站建设之 MS Sql Server数据库分区

    什么是数据库分区?数据库分区是一种对表的横向分割,Sql server 2005企业版和之后的Sql server版本才提供这种技术,这种对表的横向分割不同于2000中的表分割,它对访问用户是透明的, ...

  10. mysql 5.7.19安装

    从mysql官网下载的mysql5.7.19免安装版, 安装时出现问题,mysql总是启动不起来,在网上查了下资料,做个记录: .将mysql解压到指定的文件夹 .以管理员身份运行cmd .定位到my ...