题目链接: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. python进程池 使用Queue实现计数功能

    多进程中各个进程间相互隔离,进程间通信需要使用到通道. 多进程中使用Queue实现进程中通信 from multiprocessing import Process,Queue import time ...

  2. 一个自己稍作修改了的美赛论文 LaTeX 模板

    警告:这是旧版模板的发布页面.本站已经发布了最新版的美赛模板 easymcm(2020 年美赛可用),请到该页面查看: https://www.cnblogs.com/xjtu-blacksmith/ ...

  3. eXosip的register注册

    转载于:http://blog.sina.com.cn/s/blog_4868f98601018ioh.html 这个测试程序是从eXosip原有的测试程序改造的.原程序是tools 目录下的 sip ...

  4. [转帖]Google Chrome 解决 “您的连接不是私密连接”

    Google Chrome 解决 “您的连接不是私密连接” 和被毒霸劫持 自己搭建了 k8s的 dashboard 结果 chrome 最新版本的打不开了.. 百度了下 园友的解决方案很完美 这里面转 ...

  5. [OpenBenchMarking] AMD CPU 的性能简单对比

    来源: https://openbenchmarking.org/result/1710193-AL-EPYC7351P64 1. 2. 3. 4.

  6. windows 修改Administrator管理员账户名

      用[Win+R]组合键命令打开[运行]界面,输入[gpedit.msc],按[回车键]或[鼠标左键]单击[确定]按钮:   在弹出的[本地组策略编辑器]对话框中,依次[鼠标左键]点击打开:[计算机 ...

  7. 正式发布! .NET开发控件集ComponentOne 新版本加入Blazor UI

    近期,由葡萄城推出的ComponentOne .NET开发控件集正式发布最新版本! ComponentOne 是一套专注于企业 .NET开发.支持 .NET Core 平台,并完美集成于 Visual ...

  8. Centos7下关闭Firewalls配置iptables

    在网上搜索了很多这种资料,现在总结一下以备后用. 1.关闭防火墙:sudo systemctl stop firewalld.service 2.关闭开机启动:sudo systemctl disab ...

  9. VS code 的使用

    VS code 是一个非常不错的代码编辑器.它不依赖与任何编译器或者解释器,但却能高效的利用第三方编译器或解释器来给用户提供良好的代码编写环境. 我喜欢VS code的三点,丰富强大的插件,快捷的语法 ...

  10. Block Breaker HDU - 6699(深搜,水,写下涨涨记性)

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...