题解

挺水的一道题. Rating $ \color{orange} {2300}$ 以下送命题。

首先我们知道,所有子树大小之和就是节点个数加上从根到所有节点的路径长度之和。

他要求度数尽可能小,所以我们二分度数\(k\).显然,度数越小,子树和越大。

对于一个\(k\)叉树,他的子树大小之和为\(n+k^2+k^3+...+rem\)

我们通过二分得到最大的边界\(k\)

然后,此时我们的子树大小\(s\)是要小于等于规定的子树大小和的。

我们考虑扩大子树大小。

显然,我们让某些节点,往深度扩展将会扩大我们的子树大小。

我们记录每个深度的节点个数,已经每个节点的深度。

我们尝试从深度最深的节点开始往下扩展,直至子树大小达到规定大小。

Tips:n-1叉树的大小显然为2n-1 而一条链的大小为 n(n+1)/2 。如果k不在这个范围内,则无解。

具体实现非常简单。代码如下

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e6+10;
ll n,k,cnt[maxn],d[maxn];
bool check(int x) {
ll i(2),j,t=1,num=k-n,dep=0;
memset(d,0,sizeof d);
memset(cnt,0,sizeof cnt);
while(i<=n) {
t*=x;++dep;
for(j=1;j<=t&&i<=n;++i,++j) cnt[dep]++,d[i]=dep,num-=dep;
}
if(num<0) return false;
j=n;
while(num) {
++dep;
if(cnt[d[j]]==1) --j;
t = min(num,dep-d[j]);
cnt[d[j]]--;
d[j]+=t;
cnt[d[j]]++;
num-=t;
--j;
}
return true;
}
int main() {
cin>>n>>k;
if(k<(1LL*2*n-1)||k>(1LL*n*(n+1)/2)) {
puts("No");
exit(0);
}
int l = 1, r = n;
while(l<r) {
int mid = (l+r)>>1;
if(check(mid)) r=mid;
else l = mid+1;
}
check(r);
puts("Yes");
int pos;
d[pos=1]=0; sort(d+2,d+1+n); memset(cnt,0,sizeof cnt);
for(int i = 2;i<=n;++i) {
while(d[pos]!=d[i]-1||cnt[pos]==r) ++pos;
cout<<pos<<' ';cnt[pos]++;
}
return 0;
}

【文文殿下】CF1098C Construct a tree 题解的更多相关文章

  1. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  2. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  3. Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java

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

  4. Construct Binary Tree from Preorder and Inorder Traversal leetcode java

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

  5. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

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

  7. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  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 OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

随机推荐

  1. Nmap参数说明

    Nmap(Network Mapper)是一款开放源代码的网络探测和安全审核工具.它的设计目标是快速地扫描大型网络,不适应于单一主机.Nmap使用检测IP数据包来确定访问的主机.提供的服务.数据包的类 ...

  2. Connecting to MQSeries with .NET

    By connecting to MQSeries withing a .NET application, first it has to be done is to install MQ Serie ...

  3. wm_concat函数

    wm_concat函数   wm_concat函数 一般分类 — 作者 zzy020128 @ 12:21 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以",& ...

  4. mysql批量数据导入探究

    最近工作碰到一个问题,如何将大量数据(100MB+)导入到远程的mysql server上. 尝试1: Statement执行executeBatch的方法.每次导入1000条记录.时间为12s/10 ...

  5. oracle 存储过程应用

    1.查看 SELECT * FROM all_source WHERE type='PROCEDURE' and name=upper('liuyi_prcd'); 2.删除 DROP PROCEDU ...

  6. 2018.10.01 NOIP模拟 卡牌游戏(贪心)

    传送门 简单贪心题. 然而考试的时候失了智少讨论了一种情况导致gg. 实际上用到了二分图匹配的思想,L每次找到刚好比当前的牌小一点的出出去,看能匹配几个. 如何处理? 我们先考虑第一种比分策略. 我们 ...

  7. Repository模式中,Update总是失败及其解析(转)

    出处:http://www.cnblogs.com/scy251147/p/3688844.html 关于Entity Framework中的Attached报错的完美解决方案终极版 前发表过一篇文章 ...

  8. head first 设计模式文摘

    1 欢迎来到设计模式世界:设计模式入门 2 让你的对象知悉现况:观察者模式 3 装饰对象:装饰者模式 4 工厂模式:烘烤OO的精华 5 单件模式:独一无二的对象 6 命令模式:封装调用 7 适配器模式 ...

  9. C++ 动态分配 和 内存分配和内存释放

    动态分配 动态分配可以说是指针的关键所在.不需要通过定义变量,就可以将指针指向分配的内存.也许这个概念看起来比较模糊,但是确实比较简单.下面的代码示范如何为一个整数分配内存: int *pNumber ...

  10. 揭开AutoRun功能的神秘面纱

    有很多光盘放入光驱就会自动运行,它们是怎么做到的呢?光盘一放入光驱就会自动被执行,主要依靠两个文件,一是光盘上的AutoRun.inf文件,另一个是操作系统本身的系统文件之一的Cdvsd.vxd.Cd ...