1. 程式人生 > >CodeForces - 1098.DIV1.C: Construct a tree(貪心,構造)

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">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">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">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 (2n105; 1s1010

).

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 Input
3 5
Output
Yes
1 1 
Input
4 42
Output
No
Input
6 15
Output
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=1000010;ll N,S;
ll fa[maxn],q[maxn],d[maxn],head,tail,sz[maxn],son[maxn];
bool check(ll Mid)
{
    ll tN=N,now=1,p=1,res=0;
    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-1; ll Mx=N*(N+1)/2;
    if(S<Mn||S>Mx) return puts("NO"),0;
    ll L=1,R=N-1,Mid,res;
    while(L<=R){
        Mid=(L+R)/2;
        if(check(Mid)) res=Mid,R=Mid-1;
        else L=Mid+1;
    }
    puts("YES");
    rep(i,1,N) sz[i]=1; ll Now=Mx,D=2;
    for(int i=N;;i--){
        if(Now==S) break;
        if(sz[D]==sz[D-1]*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=1; q[head]=1; d[1]=1;
    ll p=1;
    rep(i,2,N) {
        if(sz[i]==0) break;
        L=p+1; R=p+sz[i];
        rep(j,L,R){
            while(d[q[head]]!=i-1||son[q[head]]==res){
                head++;
            }
            fa[j]=q[head]; son[q[head]]++;
            q[++tail]=j; d[j]=i;
        }
        p=R;
    }
    rep(i,2,N) printf("%lld ",fa[i]);
    return 0;
}

 

相關推薦

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 wi

Codeforces-1076EVasya and a Tree樹狀陣列

E. Vasya and a Tree time limit per test 2 seconds memory limit per test 256 megabytes inputstandard input outputstandard output Vas

Codeforces 915F Imbalance Value of a Tree並查集

路徑 second long long air bit force 題意 for href 題目鏈接 Imbalance Value of a Tree 題意 給定一棵樹。求樹上所有簡單路徑中的最大權值與最小權值的差值的和。 首先考慮求所有簡單路徑中的最大權值

Codeforces 1076E Vasya and a Tree樹狀陣列

題意:給你一顆以1為根節點的樹,初始所有節點的權值為0,然後有m個操作,每個操作將點x的所有距離不超過d的節點權值+1,問經過m次操作後每個節點權值是多少? 思路:如果是一個序列,就可以直接用樹狀陣列做,但這是一顆樹,所以我們可以想辦法把它轉化成序列。我們可以先求出每個節點的dfs序,以及深度和子樹的大小,

Codeforces Round #390 (Div. 2)(A,B,C(記憶化搜尋),D貪心優先佇列)

/* Codeforces Round #390 (Div. 2) 時間: 2017/02/16 A. Lesha and array splitting 題意:將集合分成幾個小集合,要求小集合的和不為0. 題解:遍歷過去,一直到不滿足集合並數字非0前生成一個集合 */ #

Codeforces-1059ESplit the Tree貪心+倍增

E. Split the Tree time limit per test 2 seconds memory limit per test 256 megabytes inputstandard input outputstandard output You a

2017廣西邀請賽 Query on A Tree 可持續化字典樹

題意 second for each follow n) nod pair content back Query on A Tree 時間限制: 8 Sec 內存限制: 512 MB提交: 15 解決: 3[提交][狀態][討論版] 題目描述 Monkey

hdu 6191--Query on A Tree持久化字典樹

out trie scribe nodes include mathjax osi lan push_back 題目鏈接 Problem Description Monkey A lives on a tree, he always plays on this t

HDU-1325-Is It A Tree?並查集

Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directe

CF Div2 E. Vasya and a Tree思維 + 線段樹

題目連結: E. Vasya and a Tree   題意: 給定一個以 1 為根節點的樹,初始每個節點的權值為 0 。有 m 次操作,每次把以 vi 為祖先且離 vi 的距離小於 di 的所有節點(包括 vi 本身)的權值加上 xi 。問所有操作結束後,每個節點的權

hdu 1325 Is It A Tree? 並查集

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between n

Codeforces Round #514 (Div. 2) E. Split the Tree 貪心 + 樹上倍增

題目大意:給出一棵有 n 個結點的樹,每個結點都有一個權值 w ,現在要你將這棵樹分成若干條鏈,且每個結點只能屬於一條鏈,分出來的鏈滿足每條鏈上的結點不超過L個,同時這些結點的權值和不超過S。問你最少能把這棵樹分成幾條鏈。 題目思路:由於是要使得鏈儘可能的少,所以分出來

P2633 Count on a tree樹上主席樹

n-1 pos space 求解 轉化 for clas otl -a 思路 運用樹上差分的思想,轉化成一個普通的主席樹模型即可求解 代碼 #include <cstdio> #include <algorithm> #include <cst

Is It A Tree?並查集

題目:   給出一對對的數字a,b,表示從a到b有一條邊。判斷這是不是一棵樹。 多case,每個case以0 0 結尾輸入以-1 -1結尾分析:①每個節點(除了根結點)只有一個入度;②只有一個根結點。 森林:多個根。 入度:指向同一個結點的邊數。 將點逐個加入集合中,然後判

hdu-1055----樹染色問題color a tree貪心

題目Color a Tree Problem Description Bob is very interested in the data structure of a tree. A tree is a directed graph in wh

kuangbin專題五並查集 POJ1308 Is It A Tree?並查集

題意: 與小希的迷宮一樣。 題解: 參考一下小希的迷宮。 #include<stdio.h> #include<string.h> #include<algorithm> using namespa

樹染色問題color a tree貪心

題目Color a Tree Problem Description Bob is very interested in the data structure of a tree. A tree is a directed graph in which a special node is sin

C#LBP特徵影象VS2010窗體+程式碼

        private Bitmap xjGetLBP(Bitmap BitmapOld)        {            //原始LBP            int xjWidth = BitmapOld.Width;//寬度            int

Day1循環語句WhileFor

int all any else big for循環 got it! 小程序 一、while循環   while 條件:     條件為真執行的語句   esle:     條件為假執行的語句    #!/usr/bin/env python # -*- coding:u

es6module模塊exportimport

導入 運行時 發現 let 腳本文件 推薦 必須 哪些 書寫 es6之前,社區模塊加載方案,主要是CommonJS(用於服務器)和AMD(用於瀏覽器) 而es6實現的模塊解決方案完全可以替代CommonJS和AMD ES6模塊設計思想:盡量靜態化,在編譯時就能確定模塊的依