Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)
题目链接: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(构造)的更多相关文章
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- 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 ...
- Codeforces Round #509 (Div. 2)
咕咕咕了好多天终于有时间写篇博客了_(:з」∠)_ 打网赛打到自闭的一周,终于靠这场CF找回了一点信心... 1041A - Heist \(ans=max\left \{ a_i \right \} ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化
C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...
- 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 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- 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 ...
- 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 ...
随机推荐
- XKC's basketball team【线段树查询】
XKC , the captain of the basketball team , is directing a train of nn team members. He makes all mem ...
- Servlet的request和response
SERVLET API中forward() 与redirect()的区别? 答:前者仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址:后者则是完全的跳转,浏览器将会得到跳转的地址 ...
- 关于使用pietty或putty终端连接ubuntu虚拟机时报被拒绝连接问题
首先如果要使用终端进行远程连接的ubuntu虚拟机的话,必须保证其虚拟机ip能在window下ping的动.具体的ubuntu网络配置这里不再讲,我这里使用的是NAT连接. 然后检查ssh服务是否有安 ...
- .Net Core 3.0原生Json解析器
微软官方博客中描述了为什么构造了全新的Json解析器而不是继续使用行业准则Json.Net 微软博客地址:https://devblogs.microsoft.com/dotnet/try-the-n ...
- (六)mybatis 全局配置文件讲解
目录 properties (属性) settings 全局参数配置 typeAliases 别名设置 typeHandlers 类型处理器 mapper (映射器) 细节 properties (属 ...
- fiddler笔记:快捷工具栏
WinConfig: Comment 为所有选中的Session添加Comment. Replay Replay+ctrl 重新发送请求,而不包括任何条件请求头. Replay+shift 指定每 ...
- hdu 1151 最小路径覆盖
先说说最小路径覆盖的定义 定义:在一个有向图中,找出最少的路径,使得这些路径,经过每一个点,且每一个点只与一条路径相关联, 由上面得出: 1.一个单独的点是一个路径 2:如果有路径a,b,c....f ...
- 【原创】大叔经验分享(77)openresty(nginx+lua)发http请求
openresty(nginx+lua)发http请求 利用location+proxy_pass间接实现 location ^~ /test/http { internal; proxy_pass ...
- spark2.0的10个特性介绍
1. Spark 2.0 ! 还记得我们的第七篇 Spark 博文里吗?里面我用三点来总结 spark dataframe 的好处: 当时是主要介绍 spark 里的 dataframe,今天是想总结 ...
- JS 发送弹幕
JS实现弹幕的发送 <div class="box1"> <div class="box2" style="width: 600px ...