結果

問題 No.898 tri-βutree
ユーザー kappybarkappybar
提出日時 2020-04-07 14:54:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 524 ms / 4,000 ms
コード長 2,009 bytes
コンパイル時間 2,228 ms
コンパイル使用メモリ 177,812 KB
実行使用メモリ 27,584 KB
最終ジャッジ日時 2023-08-08 16:44:01
合計ジャッジ時間 12,157 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 310 ms
27,584 KB
testcase_01 AC 5 ms
6,408 KB
testcase_02 AC 5 ms
6,384 KB
testcase_03 AC 5 ms
6,380 KB
testcase_04 AC 5 ms
6,504 KB
testcase_05 AC 5 ms
6,448 KB
testcase_06 AC 5 ms
6,444 KB
testcase_07 AC 485 ms
21,608 KB
testcase_08 AC 488 ms
21,912 KB
testcase_09 AC 498 ms
21,900 KB
testcase_10 AC 496 ms
21,984 KB
testcase_11 AC 490 ms
21,992 KB
testcase_12 AC 507 ms
21,844 KB
testcase_13 AC 488 ms
21,744 KB
testcase_14 AC 498 ms
21,788 KB
testcase_15 AC 485 ms
21,980 KB
testcase_16 AC 524 ms
21,744 KB
testcase_17 AC 493 ms
21,720 KB
testcase_18 AC 488 ms
21,788 KB
testcase_19 AC 491 ms
21,668 KB
testcase_20 AC 507 ms
21,848 KB
testcase_21 AC 491 ms
21,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll =  long long ;
using P = pair<ll,ll> ;
const ll INF = 10000000000;
const int MOD = 1000000007;


int v = 100005; //頂点の数
vector<vector<P>> tree(v,vector<P>()); //木
vector<vector<int>> parents; //parents[i][j] := i の 2^j 上の祖先
vector<int> depth(v,0); //rootからの深さ
vector<ll> dist(v,0);

void init_dfs(int i,int before=-1,int d=0,ll dis=0){
    parents[i][0] = before;
    depth[i] = d;
    dist[i] = dis;
    for(P next:tree[i]){
        if(next.second == before) continue;
        init_dfs(next.second,i,d+1,dis+next.first);
    }
}

void LCAinit(int root = 0){
    int k = 1;
    while((1<<k)<v) k ++;
    parents.assign(v,vector<int>(k,-1));
    init_dfs(root);
    for(int j = 0;j < k-1;j++){
        for(int i = 0;i < v;i++){
            if(parents[i][j] < 0){
                parents[i][j+1] = -1;
            }else{
                parents[i][j+1] = parents[parents[i][j]][j];
            }
        }
    }
}

int LCA(int x,int y){
    if(depth[x] < depth[y]) swap(x,y);
    int k = parents[0].size();
    for(int i = 0;i < k;i++){
        if((depth[x]-depth[y]) >> i & 1){
            x = parents[x][i];
        }
    }

    if(x == y) return x;
    for(int i = k-1;i >= 0 ;i--){
        if(parents[x][i] != parents[y][i]){
            x = parents[x][i];
            y = parents[y][i];
        }
    }
    return parents[x][0];
}


int main(){
    cin >> v;
    rep(i,v-1){
        ll u,v,w;
        cin >> u >> v >> w;
        tree[u].push_back(P(w,v));
        tree[v].push_back(P(w,u));
    }
    LCAinit();

    /*
    cout << endl;
    rep(i,v) cout << dist[i] << " " ;
    cout << endl;
    */

    int q;
    cin >> q;
    while(q--){
        int x,y,z;
        cin >> x >> y >> z;
        ll res = dist[x]+dist[y]+dist[z];
        //cout << res << endl;
        res -= dist[LCA(x,y)] + dist[LCA(y,z)] + dist[LCA(z,x)];
        cout << res << endl;
    }

    return 0;
}
0