結果

問題 No.1752 Up-Down Tree
ユーザー rianoriano
提出日時 2021-11-06 13:04:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 2,867 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 176,796 KB
実行使用メモリ 23,852 KB
最終ジャッジ日時 2023-08-30 06:29:16
合計ジャッジ時間 4,803 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
23,852 KB
testcase_01 AC 148 ms
23,780 KB
testcase_02 AC 100 ms
15,292 KB
testcase_03 AC 96 ms
15,436 KB
testcase_04 AC 126 ms
19,708 KB
testcase_05 AC 129 ms
21,312 KB
testcase_06 AC 124 ms
18,284 KB
testcase_07 AC 133 ms
18,228 KB
testcase_08 AC 81 ms
12,764 KB
testcase_09 AC 117 ms
14,928 KB
testcase_10 AC 3 ms
7,104 KB
testcase_11 AC 4 ms
6,972 KB
testcase_12 AC 3 ms
7,024 KB
testcase_13 AC 51 ms
10,560 KB
testcase_14 AC 63 ms
11,496 KB
testcase_15 AC 14 ms
8,104 KB
testcase_16 AC 58 ms
11,164 KB
testcase_17 AC 48 ms
10,576 KB
testcase_18 AC 131 ms
15,208 KB
testcase_19 AC 129 ms
15,228 KB
testcase_20 AC 4 ms
7,112 KB
testcase_21 AC 3 ms
7,096 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘void dfs_dp(Graph&, int, int)’ 内:
main.cpp:82:9: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   82 |     auto[st,en] = euler_time[s];
      |         ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Pr pair<ll,ll>
#define all(v) v.begin(),v.end()
#define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr)
using Graph = vector<vector<ll>>;

//segment tree
//1-indexed
class segtree {
public:
    ll n;
    vector<ll> A;
    segtree(ll k){
        n = 1;
        while(n<k){ n *= 2; }
        A=vector<ll>(2*n,0);
    }
    
    //a[i]をxにする
    void replace(ll i,ll x){
        int index = n-1+i;
        A[index] = x;
        while(index>1){
            index /= 2;
            A[index] = max(A[2*index],A[2*index+1]);
        }
    }

    //i-jまでの最大値を求める
    ll mx(ll i,ll j){
        return rangemx(i,j,1,1,n);
    }

    // a,b求める区間 k区間番号 c,d区間の始終点(k=1,c=1,d=nで入力する)
    ll rangemx(ll a,ll b,ll k,ll c,ll d){
        //単位元の設定
        ll el = 0;
        if(d<a||b<c){
            return el;
        }
        else if(a<=c&&d<=b){
            return A[k];
        }
        else{
            //2区間に分割して演算を行う
            ll p = max(rangemx(a,b,k*2,c,(c+d)/2),rangemx(a,b,k*2+1,(c+d)/2+1,d));
            return p;
        }
    }

};

segtree dp(200001);
ll base = 1e6; //maxのindexを特定するためにsegtree内で (value*base+index) で保持する

//dfs
//s:始点 t:始点からの距離 i:dfs回数
vector<int> vis; int t;
vector<Pr> euler_time;
void dfs(Graph &G, int s,int i){
    t++;
    ll st = t;
    for(int nx:G[s]){
        if(vis[nx]==i) continue;
        vis[nx] = i;
        dfs(G,nx,i);
    }
    t++;
    ll en = t;
    euler_time[s] = make_pair(st,en);
}

void dfs_dp(Graph &G, int s,int i){
    for(int nx:G[s]){
        if(vis[nx]==i) continue;
        vis[nx] = i;
        dfs_dp(G,nx,i);
    }
    auto[st,en] = euler_time[s];
    
    //最大から2つ取得
    ll m1 = dp.mx(st,en);
    ll m1_val = m1/base; ll m1_idx = m1%base;
    dp.replace(m1_idx,0);
    ll m2 = dp.mx(st,en);
    ll m2_val = m2/base; ll m2_idx = m2%base;
    dp.replace(m2_idx,0);
    //両方正の偶数の場合の処理
    if(m1_val%2==0&&m2_val>0&&m2_val%2==0){
        m2_val--;
        dp.replace(m2_idx,base+m2_idx);
    }
    //この頂点の行きがけ順番号(st)に値を格納
    dp.replace(st,(m1_val+m2_val+1)*base+st);
}


int main() {
    riano_; ll ans = 0;
    ll N; cin >> N;
    Graph G(N+1);
    rep(i,N-1){
        ll a,b; cin >> a >> b;
        G[a].push_back(b); G[b].push_back(a);
    }
    
    vis.assign(N+1,-1);
    euler_time.assign(N+1,make_pair(0,0));
    
    //1を始点としてdfs(euler tour順の番号を求める)
    vis[1] = 0; t = 0; 
    dfs(G,1,0); 
    //dfs順にdp操作を行う
    vis[1] = 1;
    dfs_dp(G,1,1);
    
    ans = dp.mx(1,2*N)/base;   
    cout << ans << endl;
}
0