题意:给定nnn和ddd,构造或判断无法构造一棵二叉树使得所有点的深度(定义为到根距离)之和为ddd。

n,d≤5000n,d\leq 5000n,d≤5000

显然可以算出有解的ddd的下界和上界,分别是完全二叉树和链的情况。下面会证明在这个范围内一定有解。

考虑增量,先构造出一条链,每次选一个叶结点接到深度小111的位置。

具体实现可以按编号暴力枚举叶结点,然后暴力枚举新位置的父亲(即深度小222且儿子数<2<2<2)。如果找不到,因为它的祖先都遍历过,感性理解,这个叶结点上面都放满了,以后不可能再用,打上标记,下次增量的时候不访问。

一棵二叉树是完全二叉树,当且仅当所有倒数第三深的点都已经有两个儿子。所以不是完全二叉树时可以按上面的方法增量。

复杂度看似是O(n(n2−d))O(n(n^2-d))O(n(n2−d)),但实际上ddd小于下界时可以直接输出NO

显然下界是O(nlog⁡n)O(n\log n)O(nlogn)的,也就是说只需要考虑nlog⁡n<dn\log n<dnlogn<d

变换得n<dlog⁡nn<\frac{d}{\log n}n<lognd​

那么复杂度不劣于O((dlog⁡n)3)O((\frac{d}{\log n})^3)O((lognd​)3)

当d=5000d=5000d=5000,nnn最小只能到根号级别

加上算法复杂度不满,可以通过

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#define MAXN 5005
using namespace std;
inline int read()
{int ans=0;char c=getchar();while (!isdigit(c)) c=getchar();while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();return ans;
}
int fa[MAXN],cnt[MAXN],sum[MAXN],dep[MAXN],bad[MAXN];
int main()
{sum[1]=1;for (int i=2;i<MAXN;i++) sum[i]=i+sum[i/2]+sum[i-i/2-1];for (int i=1;i<MAXN;i++) sum[i]-=i;for (int T=read();T;T--){int n,d;n=read(),d=read();int cur=n*(n-1)/2;if (d<sum[n]||d>cur) {puts("NO");continue;}fa[1]=0;for (int i=2;i<=n;i++) fa[i]=i-1;for (int i=1;i<n;i++) cnt[i]=1;cnt[n]=0;for (int i=1;i<=n;i++) bad[i]=0,dep[i]=i-1;while (cur>d){int u;for (u=1;u<=n;u++)if (!cnt[u]&&!bad[u])break;bad[u]=1;for (int p=1;p<=n;p++)if (cnt[p]<2&&dep[u]-dep[p]==2){--cnt[fa[u]],fa[u]=p;dep[u]=dep[p]+1,++cnt[p];bad[u]=0,--cur;break;}}puts("YES");for (int i=2;i<=n;i++) printf("%d%c",fa[i]," \n"[i==n]);}return 0;
}

【CF1311E】Construct the Binary Tree【增量构造】【复杂度证明】相关推荐

  1. Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...

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

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

  3. Construct Maximum Binary Tree

    构建一颗「最大树」. 注意consruct的时候最后的return root; 我参考了serialize and deserialize binary tree的build tree 的过程. 这周 ...

  4. HDU - 5573 Binary Tree(思维+构造+二进制)

    题目链接:点击查看 题目大意:给出一棵无限大的完全二叉树,每次给出一个 n 和一个 k ,要求从点 1 出发一直向下,找到一条长度为 k 的路径,对于路径上的点可以加上其编号,也可以减去其编号,需要构 ...

  5. 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 后序的最后一个节点为根节点,在中序遍历中 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

最新文章

  1. vs项目移植到linux运行,VS2008项目移植到Linux
  2. python经典算法题:无重复字符的最长子串
  3. 中间件系列「三」netty之NIO基础
  4. 面试题:彻底理解ThreadLocal 索引的利弊 背1
  5. java北大青鸟试题_10个常见2020春招Java面试题及注意事项!
  6. 系统更新链接服务器超时,win10系统更新导致Dr.com连接认证服务器超时的解决方法...
  7. Atitit 信用管理概论 attilax学习心得
  8. OPPO R9KM手机刷机救砖线刷包附驱动教程
  9. 从空运物流到无人机物流,圆通将战火烧到了时效件
  10. java视频生成缩略图_Java调用ffmpeg工具生成视频缩略图实例
  11. win7连接远程服务器特别慢,技术员告诉您win7远程桌面连接速度慢的具体解决法子...
  12. 【更新】VMware虚拟机黑群晖7.1.1 RC(懒人包)
  13. 荷兰国旗问题(利用基数排序思想实现)
  14. 微软rd服务器,远程桌面rd服务器
  15. 扫雷小游戏最详细讲解【C语言】
  16. Intellij IDEA中修改Maven项目的项目名称
  17. 岛屿最大面积 leetcode Java_LeetCode刷题记录——岛屿的最大面积
  18. iOS 开发笔记-获取某个APP素材
  19. python正则表达式编译_用Python编译正则表达式
  20. 阿里云mysql怎么查看数据库_阿里云服务器如何查看数据库

热门文章

  1. mft文件记录属性头包括_关于NTFS-MFT
  2. wms地图绘制工具_移情地图,了解用户需求的利器
  3. 静止的人如何看待高速运动的球?
  4. 年轻人不讲武德有多可怕?
  5. 50张图,带你认识大学各专业
  6. 超越Linux!华为鸿蒙明年将成“第五大操作系统”,网友:何时超过iOS?
  7. 荷兰人发明的新客机是劈叉的!乘客坐在机翼上
  8. 编程到底难在哪里? 从一个美国实习生的故事说起
  9. java日期加减秒_Java日期——年、月、日、时、分、秒、周加减计算
  10. php psr 编码规范_PHP之PSR-4规范:自动加载