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;
} //1
for (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 (构造,就是个模拟)的更多相关文章
- [CF1311E] Construct the Binary Tree - 构造
Solution 预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案 递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之 现 ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- [Algorithm] Construct a Binary Tree and Binary Search
function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...
- 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...
- CF1311E Construct the Binary Tree
膜这场比赛的 \(rk1\) \(\color{black}A\color{red}{lex\_Wei}\) 这题应该是这场比赛最难的题了 容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所 ...
- 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 ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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 ...
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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 ...
- [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
随机推荐
- MTK Android Driver :Lcm
MTK Android Driver :lcm 1.怎样新建一个LCD驱动 LCD模组主要包括LCD显示屏和驱动IC.比如LF040DNYB16a模组的驱动IC型号为NT35510.要在MTK6577 ...
- split(resource,limit) 中limit 的含义
limit 参数控制模式应用的次数,因此影响结果数组的长度.如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后项将包含超出最后匹配的定界符的所有输入 ...
- 【python实现卷积神经网络】全连接层实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- undefined 和 not defined
概念上的解释: undefined是javascript语言中定义的五个原始类中的一个,换句话说,undefined并不是程序报错,而是程序允许的一个值. not defined是javascript ...
- \r\n的意思
\n是换行,英文是New line.\r是回车,英文是Carriage return. 1.换行符(line break),是一种计算机语言表达方式,它的作用是跳到下一个新行.在不同的语言中,代码也有 ...
- 统计子串数量,Python基础
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:陈YL PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...
- 通达OA任意用户登录 漏洞复现
0x00 漏洞简介 通达OA国内常用的办公系统,使用群体,大小公司都可以,其此次安全更新修复的高危漏洞为任意用户登录漏洞.攻击者在远程且未经授权的情况下,通过利用此漏洞,可以直接以任意用户身份登录到系 ...
- 如何利用python实现报表自动化?让你更高效的完成工作内容
如果能够实现报表自动化,那我们将节约不少的时间,更高效的完成工作内容.那么,如何利用python实现报表自动化呢?本文将介绍xlwt .xlrd.xlutils的常用功能,xlwt写Excel时公式的 ...
- Say goodbye
Since September 28th 2015 Scriptogram officially closed. We considered every option before making th ...
- Jmeter与LoadRunner的比较
一.与Loadrunner的比较相似点 1.Jmeter的架构跟loadrunner原理一样, 都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务 ...