codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合
You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d.
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last different from v vertex on the path from the root to the vertex v. The depth of the vertex v is the length of the path from the root to the vertex v. Children of vertex v are all vertices for which v is the parent. The binary tree is such a tree that no vertex has more than 2 children.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.
The only line of each test case contains two integers n and d (2≤n,d≤5000) — the number of vertices in the tree and the required sum of depths of all vertices.
It is guaranteed that the sum of n and the sum of d both does not exceed 5000 (∑n≤5000,∑d≤5000).
Output
For each test case, print the answer.
If it is impossible to construct such a tree, print “NO” (without quotes) in the first line. Otherwise, print “{YES}” in the first line. Then print n−1 integers p2,p3,…,pn in the second line, where pi is the parent of the vertex i. Note that the sequence of parents you print should describe some binary tree.
Example
inputCopy
3
5 7
10 19
10 18
outputCopy
YES
1 2 1 3
YES
1 2 3 3 9 9 2 1 6
NO
Note
Pictures corresponding to the first and the second test cases of the example:
丫的,改了一天。
如果b在构造的树的深度最大(左偏或右偏树)和最小(满二叉树)之内就能构成,然后从左偏树开始不断的将低端的点向上移动,知道达到要求。
#include <bits/stdc++.h>
using namespace std;
int f[210];
inline void solve()
{memset(f, 0, sizeof(f));int n, d, maxd = 0;scanf("%d %d", &n, &d);--n;if (d > n * (n + 1) / 2){printf("NO\n");return;} //1for (int i = 1;; ++i){maxd = i;if (n > (1 << i)){d -= i * (1 << i);f[i] = 1 << i;n -= 1 << i;}else{d -= i * n;f[i] = n;n -= n;break;}}if (d < 0){printf("NO\n");return;}while (1){if (d == 0)break;int p;for (p = maxd; p >= 1; --p)if (f[p] > 1)break;--d;--f[p];++f[p + 1];if (p + 1 > maxd)maxd = p + 1;}printf("YES\n");int p = 1, np = 1, cnt;for (int i = 1; i <= maxd; ++i){int t = p;cnt = 0;for (int j = 1; j <= f[i]; ++j){++p;++cnt;if (cnt >= 3){++np;cnt = 1;} printf("%d ", np);}np = t + 1;}printf("\n");
}
int main()
{int t;scanf("%d", &t);for (int i = 1; i <= t; ++i)solve();return 0;
}
codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)相关推荐
- Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造
传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- Construct Maximum Binary Tree
构建一颗「最大树」. 注意consruct的时候最后的return root; 我参考了serialize and deserialize binary tree的build tree 的过程. 这周 ...
- 【CF1311E】Construct the Binary Tree【增量构造】【复杂度证明】
题意:给定nnn和ddd,构造或判断无法构造一棵二叉树使得所有点的深度(定义为到根距离)之和为ddd. n,d≤5000n,d\leq 5000n,d≤5000 显然可以算出有解的ddd的下界和上界, ...
- Given inorder and postorder traversal of a tree, construct the binary tree
输入某二叉树的中序遍历和后序遍历的结果,请重建出该二叉树. 例如如下二叉树 中序:8 4 2 5 1 6 9 3 7 后序:8 4 5 2 9 6 7 3 1 后序的最后一个节点为根节点,在中序遍历中 ...
- 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 ...
- 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 ...
- LeetCode OJ: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 ...
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法
题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode Given preorder and inorder ...
最新文章
- feign调用如何传递token_走进Spring Cloud之五 eureka Feign(服务调用者)
- 推荐8个值得每天一看的网站,值得收藏起来!
- LetCode 70. Climbing Stairs--动态规划-爬梯子--递归等解法
- configure: error: no acceptable C compiler found in $PATH
- python是用c写的吗-Python是编写人工智能最佳的编程语言吗?
- html5变动标签新写法,Html5新标签解释及用法
- 【软件工程】第一章重点
- deepin下Clion连接mysql_CLion如何添加依赖库 ? 需要把mysql/Connector c++放入 用cpp连接数据库...
- 【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交)
- 不用网关或代理的单点远程办公如何实现,Aruba推出EdgeConnect Microbranch
- 推荐一个站点:里面有很多不错的长文
- 程控电源测试使用小记
- 史上最详细的接口测试,一篇学会接口
- 【杂】Excel中匹配筛选操作VLOOKUP 函数使用问题排查
- 计算机快捷键word,电脑中怎样自定义Word2010快捷键【图文教程】
- python控制蓝牙pybluez_Python之蓝牙通讯模块pybluez学习笔记
- 全球最小的一款P2P软件——eMuleBT软件框架分析
- 沉睡者IT - 短视频简单无脑玩法,播放量10w+的藏头诗玩法,操作思路分享给你!
- linux上执行sql乱码,linux sqlplus乱码怎么办
- 计算机中英文打字文章,中英文混合文章