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 ...
随机推荐
- C语言输出菱形
#include<stdio.h> #include<string.h> int main(){ int data[7][7] = {0}; for( ...
- javascript入门 之 zTree(十四 增删查改)(二)
<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - addNodes / editName / rem ...
- sql 案例
select now();#获取当前系统时间 select now() from dual;#与Oracle兼容 show character set;#产看当前数据库支持的字符集 create da ...
- 一个java 码手 的老牛 --- 涉及 一些不错的java 基础课程
http://www.zuidaima. com/user/1550463811307520/share/collect.htm
- Java学习成长第一集
由于最近所在项目组的项目临近结尾,所以有时间对自己近来的学习做个总结.不得不说,程序员不学习就退步这句话是真的很让人信服!自己入行将近一年的时间,所学的就是Java开发的专业,很羞愧的是现在的自己能力 ...
- F - Distinct Numbers
链接:https://atcoder.jp/contests/abc143/tasks/abc143_f 题解:开两个数组,其中一个arr用来保存每个元素出现的次数,同时再开一个数组crr用来保存出现 ...
- mac上搭建mysql环境配置和Navicat连接mysql
mac上搭建mysql环境配置 1.下载mysql for mac: https://downloads.mysql.com/archives/community/ 注意:mysql版本要和你的MAC ...
- 爬虫与反爬相生相克,道高一丈魔高一尺,如何隐藏ID(附代码)
Python 反爬篇之 ID 混淆 作为爬虫的一方,如果知道了某个站点的数据自增 ID,那么就能轻而易举把整个站点都爬下来. 是不是有点耸人听闻,你去看很多大站例如油管.P 站等,他们都不会轻易把业务 ...
- Spring5:控制反转
二.Spring IOC控制反转 1:IOC推导 >传统业务调用编程 定义一个userDao接口:UserDao package com.spring; public interface Use ...
- 详细的JavaScript知识梳理和经典的一百个例题,让你掌握JavaScript
这里先做一下JavaScript知识点的梳理,具体的可领取资料 JavaScript语法: js语法.png DOM操作: DOM操作.png 数据类型 面向对象 继承 闭包 插件 作用域 跨域 原型 ...