結果

問題 No.1928 Make a Binary Tree
ユーザー HaaHaa
提出日時 2022-05-06 23:04:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 639 ms / 3,000 ms
コード長 2,463 bytes
コンパイル時間 2,046 ms
コンパイル使用メモリ 181,572 KB
実行使用メモリ 205,776 KB
最終ジャッジ日時 2023-09-20 05:22:42
合計ジャッジ時間 24,321 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
27,588 KB
testcase_01 AC 13 ms
27,472 KB
testcase_02 AC 13 ms
27,588 KB
testcase_03 AC 13 ms
27,364 KB
testcase_04 AC 13 ms
27,516 KB
testcase_05 AC 13 ms
27,476 KB
testcase_06 AC 13 ms
27,524 KB
testcase_07 AC 13 ms
27,584 KB
testcase_08 AC 13 ms
27,588 KB
testcase_09 AC 13 ms
27,468 KB
testcase_10 AC 13 ms
27,584 KB
testcase_11 AC 13 ms
27,420 KB
testcase_12 AC 13 ms
27,364 KB
testcase_13 AC 14 ms
27,408 KB
testcase_14 AC 13 ms
27,468 KB
testcase_15 AC 36 ms
28,160 KB
testcase_16 AC 31 ms
27,888 KB
testcase_17 AC 25 ms
27,968 KB
testcase_18 AC 35 ms
28,060 KB
testcase_19 AC 15 ms
27,580 KB
testcase_20 AC 26 ms
27,792 KB
testcase_21 AC 19 ms
27,844 KB
testcase_22 AC 29 ms
27,792 KB
testcase_23 AC 24 ms
27,836 KB
testcase_24 AC 34 ms
27,840 KB
testcase_25 AC 38 ms
28,048 KB
testcase_26 AC 505 ms
34,180 KB
testcase_27 AC 312 ms
31,848 KB
testcase_28 AC 344 ms
32,064 KB
testcase_29 AC 589 ms
34,920 KB
testcase_30 AC 289 ms
31,228 KB
testcase_31 AC 388 ms
32,544 KB
testcase_32 AC 568 ms
34,964 KB
testcase_33 AC 322 ms
31,796 KB
testcase_34 AC 387 ms
32,592 KB
testcase_35 AC 475 ms
35,540 KB
testcase_36 AC 571 ms
122,084 KB
testcase_37 AC 470 ms
34,964 KB
testcase_38 AC 13 ms
27,588 KB
testcase_39 AC 615 ms
120,504 KB
testcase_40 AC 615 ms
120,456 KB
testcase_41 AC 613 ms
120,452 KB
testcase_42 AC 615 ms
120,624 KB
testcase_43 AC 618 ms
120,512 KB
testcase_44 AC 616 ms
120,448 KB
testcase_45 AC 616 ms
120,464 KB
testcase_46 AC 615 ms
120,508 KB
testcase_47 AC 616 ms
120,508 KB
testcase_48 AC 617 ms
120,504 KB
testcase_49 AC 639 ms
205,776 KB
testcase_50 AC 564 ms
36,248 KB
testcase_51 AC 568 ms
36,336 KB
testcase_52 AC 565 ms
36,356 KB
testcase_53 AC 571 ms
36,352 KB
testcase_54 AC 564 ms
36,284 KB
testcase_55 AC 546 ms
124,468 KB
testcase_56 AC 589 ms
35,356 KB
testcase_57 AC 592 ms
35,228 KB
testcase_58 AC 592 ms
35,180 KB
testcase_59 AC 522 ms
38,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

template<typename X>
struct Segtree{
    using F=function<X(X,X)>;
    int n; F f; X ex;
    vector<X> dat;
    Segtree(int n_, F f_, X ex_):f(f_), ex(ex_){
        n=1;
        while(n_>n){n*=2;}
        dat.assign(2*n,ex);
    }
    void set(int i, X x){
        dat[i+n-1]=x;
    }
    void build(){
        for(int k=n-2;k>=0;k--) 
            dat[k]=f(dat[2*k+1],dat[2*k+2]);
    }
    void update(int i, X x){
        i+=n-1;
        dat[i]=x;
        while(i>0){
            i=(i-1)/2;
            dat[i]=f(dat[i*2+1],dat[i*2+2]);
        }
    }
    X query(int a, int b){
        return query_sub(a,b,0,0,n);
    }
    X query_sub(int a, int b, int k, int l, int r){
        if(r<=a||b<=l)
            return ex;
        else if(a<=l&&r<=b)
            return dat[k];
        else{
            X vl=query_sub(a,b,k*2+1,l,(l+r)/2);
            X vr=query_sub(a,b,k*2+2,(l+r)/2,r);
            return f(vl,vr);
        }
    }
};

P segf(P l, P r){
    if(l.first>r.first)
        return l;
    else
        return r;
}

VI g[210000];
Segtree<P> seg(420000,segf,{0,0});
VI lidx(210000);
VI ridx(210000);
int sidx=0;

int dfs(int v, int p){
    VI a;
    lidx[v]=sidx;
    sidx++;
    for(auto to:g[v]){
        if(to!=p){
            int ret=dfs(to,v);
            a.push_back(ret);
        }
    }
    ridx[v]=sidx;
    sidx++;
    REP(i,2){
        P sret=seg.query(lidx[v],ridx[v]);
        seg.update(sret.second,{0,sret.second});
        a.push_back(sret.first);
    }
    sort(ALL(a),greater<ll>());
    int j=lidx[v];
    for(int i=2;i<(int)a.size();i++){
        while(1){
            if(seg.query(j,j+1).first==0){
                seg.update(j,{a[i],j});
                break;
            }
            j++;
        }
    }
    return a[0]+a[1]+1;
}

int main(){
    int n; cin >> n;
    int a, b;
    REP(i,n-1){
        cin >> a >> b;
        a--, b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    REP(i,420000){
        seg.set(i,{0,i});
    }
    cout << dfs(0,-1) << endl;
    return 0;
}
0