题意

给定\(n\)和\(d\),构造一颗\(n\)个节点的二叉树(以\(1\)为根),所有节点到\(1\)的距离和为\(d\),不行输出\(NO\),否则输出\(YES\)和\(2\)-\(n\)的父亲编号

分析

最大和显然是一条链,如果最大和仍小于\(d\),则不行,否则先构造出一条链,然后枚举当前的层数,如果当前层数的下一层仍然可以填,则将链的尾端放置在下一层可得到最大的变化,如果这个变化大于所需要的变化,则找到挂置的层数,否则则挂在下一层,若下一层已满,则将所枚举层数下移

#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define int ll
#define ls st<<1
#define rs st<<1|1
#define pii pair<int,int>
#define rep(z, x, y) for(int z=x;z<=y;++z)
#define com bool operator<(const node &b)
const int maxn = (ll) 5e3 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int T = 1;
int pre[maxn];
set<int> lay[maxn];
vector<int> son[maxn]; void solve() {
int n, d;
cin >> n >> d;
int now = 0;
son[1].clear();
lay[1].clear();
lay[1].insert(1);
rep(i, 2, n) {
pre[i] = i - 1, now += i - 1, lay[i].clear();
lay[i].insert(i);
son[i].clear();
son[i - 1].push_back(i);
}
if (now < d) {
cout << "NO\n";
return;
}
int nowLay = 1;
for (int i = n; i >= 1; --i) {
if (i <= nowLay + 1)
break;
int maxx = i - nowLay - 1;
if (maxx >= now - d) {
if (lay[i - 1 - (now - d)].empty()) {
++nowLay;
++i;
continue;
}
int fa = *lay[i - 1 - (now - d)].begin();
pre[i] = fa;
now = d;
break;
} else {
now -= maxx;
int fa = *lay[nowLay].begin();
son[fa].push_back(i);
if (son[fa].size() == 2)
lay[nowLay].erase(fa);
son[pre[i]].erase(son[pre[i]].begin());
pre[i] = fa;
lay[nowLay + 1].insert(i);
lay[i].erase(i);
if (lay[nowLay].empty())
++nowLay;
}
}
if (now != d) {
cout << "NO\n";
return;
}
cout << "YES\n";
rep(i, 2, n)cout << pre[i] << ' ';
cout << '\n';
} signed main() {
start;
cin >> T;
while (T--)
solve();
return 0;
}

CodeForces 1311E Construct the Binary Tree的更多相关文章

  1. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  2. [Algorithm] Construct a Binary Tree and Binary Search

    function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...

  3. 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)

    题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...

  4. Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals

    http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...

  5. CF1311E Construct the Binary Tree

    膜这场比赛的 \(rk1\) \(\color{black}A\color{red}{lex\_Wei}\) 这题应该是这场比赛最难的题了 容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所 ...

  6. [CF1311E] Construct the Binary Tree - 构造

    Solution 预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案 递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之 现 ...

  7. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  8. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. Leetcode Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  10. LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. 【GiraKoo】C++编译中常用的内置宏

    开源项目:https://girakoo.com/ 联系方式:girakoo@163.com 简介 针对不同的平台,很多头文件,函数名称,类型占用空间不一致. 为了保证跨平台可编译,经常需要在项目中使 ...

  2. 记一次 .NET 某汽贸店 CPU 爆高分析

    一:背景 1. 讲故事 上周有位朋友在 github 上向我求助,说线程都被卡住了,让我帮忙看下,截图如下: 时隔两年 终于有人在上面提 Issue 了,看样子这块以后可以作为求助专区来使用,既然来求 ...

  3. openlayers Text字体大小设置

    今做一个app版的ol地图,发现区域太小显示拥挤,于是想把字体改小,看起来匀称点,于是盯紧了font属性使劲改 老是不听咋整呢?网上找大佬 于是找到了得到了各路大神鼎力相助 如: 赶紧跑去试试,原来这 ...

  4. WPF入门教程系列二十六——DataGrid使用示例(3)

    WPF入门教程系列目录 WPF入门教程系列二--Application介绍 WPF入门教程系列三--Application介绍(续) WPF入门教程系列四--Dispatcher介绍 WPF入门教程系 ...

  5. Linux 调用约定

      函数调用约定是对函数调用时如何传递参数的一种约定.关于它的约定有许多种,下面我们分 别从内核接口和用户接口两方面介绍32位和64位Linux的调用约定. 一.内核接口   x86-32系统调用约定 ...

  6. 天翼云SD-WAN解决方案直播

    2023年6月16日14点,天翼云SD-WAN解决方案直播火热来袭啦!参与直播即可领取优惠好礼,实惠多多! 点击链接注册参与:https://ctyun.d1meeting.cn/0616/ 直播时间 ...

  7. 认识Java注解

    1.注解是什么? 注解,元数据的一种形式,提供与程序有关的数据,但又不是程序的一部分.注解对它们注解的代码操作没有直接影响. 附注:元数据是指用来描述数据的数据,更通俗一点,就是描述代码间关系,或者代 ...

  8. CSS3+Jquery实现带动画效果的下拉选择框

    CSS3+JQuery实现带动画效果的下拉选择框. 元素结构为: 1 <div class="box"> 2 <p>this is the first li ...

  9. VS2017登录账户提示升级Edge浏览器的问题

    在win7系统登录VS2017账号总是提示要升级浏览器的问题,这里给大家提供一个解决办法,也是困扰我很久的问题.遇到这中问题,我们可以在VS工具->选项->账户,然后勾选在添加账户或对账户 ...

  10. SpringBoot 2 种方式快速实现分库分表,轻松拿捏!

    大家好,我是小富- (一)好好的系统,为什么要分库分表? (二)分库分表的 21 条法则,hold 住! 本文是<分库分表ShardingSphere5.x原理与实战>系列的第三篇文章,本 ...