题目链接: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. java 自带 http get/post 请求

    请求参数,请求参数应该是 name1=value1&name2=value2 的形式. import java.io.BufferedReader; import java.io.IOExce ...

  2. 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)

    Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...

  3. 用bisect来管理已排序的序列

    bisect 模块包含两个主要函数,bisect 和 insort,两个函数都利用二分查找算法来在有序序列中查找或插入元素. 2.8.1 用bisect来搜索 bisect(haystack, nee ...

  4. - 多次点击事件 MD

  5. 2017多校赛 Function

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  6. 3-MySQL DBA笔记-开发基础

    第二部分 开发篇 本篇首先讲述数据库开发的一些基础知识,如关系数据模型.常用的SQL语法.范式.索引.事务等,然后介绍编程开发将会涉及的数据库的一些技巧,最后结合生产实际,提供一份开发规范供大家参考. ...

  7. vue 安装插件

    import VueClipboard from 'vue-clipboard2' import MessagePlugin from '../message' import * as filters ...

  8. C#picturebox控件图片以json格式上传java后台保存

    关于winform上传图片到Java后端,保存到数据库,有多种方法,本文主要介绍利用picturebox控件,点击按钮上传图片,将图片转化为base64格式,以json格式上传到Java后台,再从ja ...

  9. 维护solr索引库

    一 2)solrcore    一个solr下可以有多个solrcore,每个solrcore就是一个独立的索引库3)solrconfig.xml    lib:配置solr的扩展包的位置,不指定路径 ...

  10. 移植 Linux 内核

    目录 更新记录 1.Linux 版本及特点 2.打补丁.编译.烧写.启动内核 3.内核源码文件结构 4.内核架构分析 4.1 内核配置 4.2 Makefile架构分析 4.3 Kconfig 架构文 ...