CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)
Misha walked through the snowy forest and he was so fascinated by the trees to decide to draw his own tree!
Misha would like to construct a rooted tree with n
vertices, indexed from 1 to n, where the root has index 1. Every other vertex has a parent pi, and i is called a child of vertex pi. Vertex u belongs to the subtree of vertex v iff v is reachable from u while iterating over the parents (u, pu, ppu, ...). Clearly, v belongs to its own subtree, and the number of vertices in the subtree is called the size of the subtree. Misha is only interested in trees where every vertex belongs to the subtree of vertex 1
.
Below there is a tree with 6
vertices. The subtree of vertex 2 contains vertices 2, 3, 4, 5. Hence the size of its subtree is 4
.
The branching coefficient of the tree is defined as the maximum number of children in any vertex. For example, for the tree above the branching coefficient equals 2
. Your task is to construct a tree with n vertices such that the sum of the subtree sizes for all vertices equals s
, and the branching coefficient is minimum possible.
Input
The only input line contains two integers n
and s — the number of vertices in the tree and the desired sum of the subtree sizes (2≤n≤105; 1≤s≤1010
).
Output
If the required tree does not exist, output «No». Otherwise output «Yes» on the first line, and in the next one output integers p2
, p3, ..., pn, where pi denotes the parent of vertex i
.
Examples
3 5
Yes
1 1
4 42
No
6 15
Yes
1 2 3 1 5
Note
Below one can find one of the possible solutions for the first sample case. The sum of subtree sizes equals 3+1+1=5
, and the branching coefficient equals 2
.
Below one can find one of the possible solutions for the third sample case. The sum of subtree sizes equals 6+3+2+1+2+1=15
, and the branching coefficient equals 2
.
题意:给定N,S,让你构造一个大小为N的数,使得每个节点子树大小之和为S,如果存在,请构造一个树,使得儿子最多的点的儿子数量(P)最少。
思路:我们发现对于大小一定的树,越瘦长K越大(一条链,最大为N*(N+1)/2),越矮胖越小(菊花树,最小为N+N-1),那么如果K不在这个范围我们输出-1;如果在,我们一定看i有构造一个满足题意的树。 我们可以二分得到P。然后来构造。 我的构造方式是先构造一条链,此时的sum=N*(N+1)/2;如果sum>S,我们就把最下面的点移到上面的某个位置,知道sum=S。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;ll N,S;
ll fa[maxn],q[maxn],d[maxn],head,tail,sz[maxn],son[maxn];
bool check(ll Mid)
{
ll tN=N,now=,p=,res=;
while(tN){
res+=min(p,tN)*now;
if(res>S) return false;
tN-=min(p,tN);
p*=Mid; now++;
} return true;
}
int main()
{
cin>>N>>S;
ll Mn=N+N-; ll Mx=N*(N+)/;
if(S<Mn||S>Mx) return puts("NO"),;
ll L=,R=N-,Mid,res;
while(L<=R){
Mid=(L+R)/;
if(check(Mid)) res=Mid,R=Mid-;
else L=Mid+;
}
puts("YES");
rep(i,,N) sz[i]=; ll Now=Mx,D=;
for(int i=N;;i--){
if(Now==S) break;
if(sz[D]==sz[D-]*res) D++;
if(Now-S>=i-D){
sz[D]++; sz[i]--;
Now-=(i-D);
}
else {
sz[i]--; sz[i-(Now-S)]++;
Now=S;
}
}
head=tail=; q[head]=; d[]=;
ll p=;
rep(i,,N) {
if(sz[i]==) break;
L=p+; R=p+sz[i];
rep(j,L,R){
while(d[q[head]]!=i-||son[q[head]]==res){
head++;
}
fa[j]=q[head]; son[q[head]]++;
q[++tail]=j; d[j]=i;
}
p=R;
}
rep(i,,N) printf("%lld ",fa[i]);
return ;
}
CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)的更多相关文章
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- 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 ...
- [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
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- Construct a tree from Inorder and Level order traversals
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
随机推荐
- 语言小知识-MySQL数据库引擎
MySQL作为全世界广受欢迎的数据库,被用于很多中小型的项目中,但是你对 MySQL 数据库的存储引擎了解多少呢? 我们将逻辑表中的数据存储到数据库中,数据库又将我们表中的数据存储到物理设备中(如磁盘 ...
- mint18
ubuntu16.04用了一段时间,果然遇到祖传内部错误.然后虚拟机遇到2次重启后卡死在黑屏闪光标位置.但是用系统盘准备重装,执行到分区这步放弃,重启,居然有能启动. 作为开发机,实在有点胆战心惊,虽 ...
- LeetCode--021--合并两个有序链表
问题描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...
- 『PyTorch』第五弹_深入理解autograd_下:函数扩展&高阶导数
一.封装新的PyTorch函数 继承Function类 forward:输入Variable->中间计算Tensor->输出Variable backward:均使用Variable 线性 ...
- 安装torch-opencv
安装torch-opencv torch torch-opencv opencv-3.1.0 opencv-contrib 想在torch中使用光流法,于是就希望能够调用opencv中的光流代码,而t ...
- UVA-11882 Biggest Number (DFS+剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- Mac XMind8 保存时报错
错误提示 截图 日志 查看错误日志的方式:打开xmind –> 关于xmind –> 安装细节 –> 选项卡 “配置” –> 查看错误日志 看到有Caused by: org. ...
- JS-图片控制-动画管理模块
animateManage.js ;(function(window,document,undefined){ var _aniQueue = [], //动画队列 --- ani:动画,Queue: ...
- iOS UI-UIPickerView(拾取器)、UIWebView(网页视图)和传值方式
// // ViewController.m // IOS_0107_finalToolClass // // Created by ma c on 16/1/7. // Copyright (c) ...
- 怎样解决IIS6.0上传文件限制的问题?
我们用IIS发布的Bs项目,如果进行文件上传,在上传文件的时候,无法上传文件大小超过4M的文件 设置文件上传大小的方法,就是修改项目的web.config配置 在项目中的web.config文件中,添 ...