結果

問題 No.1094 木登り / Climbing tree
ユーザー face4face4
提出日時 2020-07-05 14:11:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 724 ms / 2,000 ms
コード長 2,647 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 81,968 KB
実行使用メモリ 58,640 KB
最終ジャッジ日時 2024-04-25 19:18:50
合計ジャッジ時間 17,908 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,856 KB
testcase_01 AC 677 ms
44,048 KB
testcase_02 AC 121 ms
58,640 KB
testcase_03 AC 166 ms
10,468 KB
testcase_04 AC 156 ms
24,020 KB
testcase_05 AC 244 ms
39,648 KB
testcase_06 AC 340 ms
19,308 KB
testcase_07 AC 693 ms
44,260 KB
testcase_08 AC 687 ms
44,168 KB
testcase_09 AC 658 ms
44,172 KB
testcase_10 AC 693 ms
44,172 KB
testcase_11 AC 686 ms
44,172 KB
testcase_12 AC 666 ms
44,172 KB
testcase_13 AC 674 ms
44,168 KB
testcase_14 AC 686 ms
43,972 KB
testcase_15 AC 367 ms
19,444 KB
testcase_16 AC 526 ms
46,980 KB
testcase_17 AC 430 ms
30,912 KB
testcase_18 AC 407 ms
25,580 KB
testcase_19 AC 478 ms
39,780 KB
testcase_20 AC 676 ms
44,184 KB
testcase_21 AC 440 ms
32,636 KB
testcase_22 AC 668 ms
44,044 KB
testcase_23 AC 668 ms
44,168 KB
testcase_24 AC 724 ms
44,180 KB
testcase_25 AC 679 ms
44,172 KB
testcase_26 AC 645 ms
44,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

typedef pair<int,int> pii;

struct LCA{
    int n, bit; // n < 1<<bit
    vector<int> depth;
    vector<vector<int>> par, v;

    LCA(int n) : n(n){
        depth.resize(n, -1);
        bit = 0;
        while(1<<bit <= n)  bit++;
        par.resize(bit);
        for(int i = 0; i < bit; i++)    par[i].resize(n);
        v.resize(n);
    }

    void add_edge(int a, int b){
        v[a].push_back(b);
        v[b].push_back(a);
    }

    void dfs(int now, int p, int d){
        par[0][now] = p;
        depth[now] = d;
        for(int child : v[now]){
            if(child == p)  continue;
            dfs(child, now, d+1);
        }
    }

    void build(){
        for(int i = 0; i < n; i++){
            if(depth[i] == -1)    dfs(i, -1, 0);
        }
        for(int k = 0; k+1 < bit; k++){
            for(int i = 0; i < n; i++){
                if(par[k][i] < 0)   par[k+1][i] = -1;
                else                par[k+1][i] = par[k][par[k][i]];
            }
        }
    }

    int lca(int a, int b){
        // make b deeper
        if(depth[a] > depth[b]) swap(a, b);
        for(int k = 0; k < 30; k++){
            // depth(b)-depth(a) is equal to or bigger than 2^k
            if(((depth[b]-depth[a])>>k)&1){
                b = par[k][b];
            }
        }
        if(a == b)  return a;
        for(int k = bit-1; k >= 0; k--){
            if(par[k][a] != par[k][b]){
                a = par[k][a];
                b = par[k][b];
            }
        }
        return par[0][a];
    }

    int dist(int a, int b){
        int l = lca(a, b);
        return depth[a]-depth[l] + depth[b]-depth[l];
    }
};

vector<pii> v[200000];
vector<int> ind(200000), val;
int k = 0;

void dfs(int x, int p){
    ind[x] = k;
    for(pii edge : v[x]){
        int to = edge.first, cost = edge.second;
        if(to == p) continue;
        val.push_back(cost);    k++;
        dfs(to, x);
        val.push_back(-cost);   k++;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    LCA l(n);
    for(int i = 0; i < n-1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
        l.add_edge(a, b);
    }
    l.build();
    val.push_back(0);
    dfs(0, -1);
    for(int i = 1; i < val.size(); i++) val[i] += val[i-1];
    int q;
    cin >> q;
    while(q--){
        int s, t;
        cin >> s >> t;
        s--, t--;
        int lca = l.lca(s, t);
        cout << val[ind[s]]-val[ind[lca]] + val[ind[t]]-val[ind[lca]] << endl;
    }
    return 0;
}
0