結果

問題 No.1928 Make a Binary Tree
ユーザー HaaHaa
提出日時 2022-05-06 22:44:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,524 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 180,472 KB
実行使用メモリ 193,264 KB
最終ジャッジ日時 2023-09-20 03:50:52
合計ジャッジ時間 15,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
27,528 KB
testcase_01 AC 13 ms
27,296 KB
testcase_02 AC 13 ms
27,588 KB
testcase_03 AC 13 ms
27,548 KB
testcase_04 AC 13 ms
27,264 KB
testcase_05 AC 13 ms
27,336 KB
testcase_06 WA -
testcase_07 AC 13 ms
27,388 KB
testcase_08 AC 13 ms
27,296 KB
testcase_09 AC 13 ms
27,268 KB
testcase_10 AC 13 ms
27,404 KB
testcase_11 AC 13 ms
27,372 KB
testcase_12 AC 13 ms
27,256 KB
testcase_13 WA -
testcase_14 AC 13 ms
27,456 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 281 ms
35,576 KB
testcase_36 AC 353 ms
115,888 KB
testcase_37 AC 181 ms
35,036 KB
testcase_38 RE -
testcase_39 AC 362 ms
114,160 KB
testcase_40 AC 361 ms
114,160 KB
testcase_41 AC 357 ms
114,236 KB
testcase_42 AC 361 ms
114,052 KB
testcase_43 AC 361 ms
114,248 KB
testcase_44 AC 360 ms
114,108 KB
testcase_45 AC 360 ms
114,084 KB
testcase_46 AC 360 ms
114,024 KB
testcase_47 AC 361 ms
114,312 KB
testcase_48 AC 357 ms
114,056 KB
testcase_49 AC 417 ms
193,264 KB
testcase_50 AC 252 ms
36,360 KB
testcase_51 AC 249 ms
36,476 KB
testcase_52 AC 250 ms
36,284 KB
testcase_53 AC 251 ms
36,148 KB
testcase_54 AC 249 ms
36,144 KB
testcase_55 AC 223 ms
118,144 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 286 ms
38,572 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){
    if(g[v].size()==1&&v!=0){
        return 1;
    }
    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++;
    sort(ALL(a),greater<ll>());
    if(a.size()==1){
        P sret=seg.query(lidx[v],ridx[v]);
        seg.update(sret.second,{0,sret.second});
        a.push_back(sret.first);
    }
    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