結果

問題 No.1928 Make a Binary Tree
ユーザー HaaHaa
提出日時 2022-05-06 22:58:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,517 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 180,204 KB
実行使用メモリ 205,824 KB
最終ジャッジ日時 2023-09-20 04:54:19
合計ジャッジ時間 21,515 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
27,432 KB
testcase_01 AC 13 ms
27,480 KB
testcase_02 AC 13 ms
27,548 KB
testcase_03 AC 14 ms
27,428 KB
testcase_04 AC 13 ms
27,340 KB
testcase_05 AC 14 ms
27,464 KB
testcase_06 AC 14 ms
27,480 KB
testcase_07 AC 13 ms
27,432 KB
testcase_08 AC 14 ms
27,432 KB
testcase_09 AC 19 ms
27,340 KB
testcase_10 AC 21 ms
27,336 KB
testcase_11 AC 14 ms
27,344 KB
testcase_12 AC 13 ms
27,336 KB
testcase_13 AC 14 ms
27,576 KB
testcase_14 AC 14 ms
27,608 KB
testcase_15 AC 32 ms
28,312 KB
testcase_16 AC 27 ms
27,860 KB
testcase_17 AC 23 ms
27,816 KB
testcase_18 AC 31 ms
27,876 KB
testcase_19 AC 15 ms
27,532 KB
testcase_20 AC 24 ms
27,888 KB
testcase_21 AC 19 ms
27,776 KB
testcase_22 AC 26 ms
27,996 KB
testcase_23 AC 22 ms
27,776 KB
testcase_24 AC 30 ms
27,804 KB
testcase_25 AC 32 ms
28,060 KB
testcase_26 AC 401 ms
34,252 KB
testcase_27 AC 247 ms
31,808 KB
testcase_28 WA -
testcase_29 AC 471 ms
34,988 KB
testcase_30 AC 230 ms
31,348 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 311 ms
32,564 KB
testcase_35 AC 478 ms
35,456 KB
testcase_36 AC 466 ms
122,204 KB
testcase_37 AC 335 ms
34,980 KB
testcase_38 AC 14 ms
27,428 KB
testcase_39 AC 548 ms
120,476 KB
testcase_40 AC 544 ms
120,432 KB
testcase_41 AC 545 ms
120,508 KB
testcase_42 AC 547 ms
120,528 KB
testcase_43 AC 550 ms
120,444 KB
testcase_44 AC 548 ms
120,268 KB
testcase_45 AC 541 ms
120,708 KB
testcase_46 AC 541 ms
120,568 KB
testcase_47 AC 546 ms
120,712 KB
testcase_48 AC 548 ms
120,444 KB
testcase_49 AC 658 ms
205,824 KB
testcase_50 AC 457 ms
36,484 KB
testcase_51 AC 441 ms
36,420 KB
testcase_52 AC 449 ms
36,244 KB
testcase_53 AC 452 ms
36,340 KB
testcase_54 AC 450 ms
36,360 KB
testcase_55 AC 436 ms
124,528 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 288 ms
38,352 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++;
    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