結果

問題 No.1094 木登り / Climbing tree
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-06-26 00:41:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 942 ms / 2,000 ms
コード長 2,344 bytes
コンパイル時間 2,083 ms
コンパイル使用メモリ 179,112 KB
実行使用メモリ 37,672 KB
最終ジャッジ日時 2024-04-25 18:31:05
合計ジャッジ時間 22,726 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 834 ms
31,656 KB
testcase_02 AC 209 ms
37,672 KB
testcase_03 AC 209 ms
6,944 KB
testcase_04 AC 189 ms
15,284 KB
testcase_05 AC 279 ms
28,072 KB
testcase_06 AC 418 ms
11,628 KB
testcase_07 AC 839 ms
31,652 KB
testcase_08 AC 825 ms
31,652 KB
testcase_09 AC 832 ms
31,656 KB
testcase_10 AC 845 ms
31,784 KB
testcase_11 AC 864 ms
31,784 KB
testcase_12 AC 904 ms
31,656 KB
testcase_13 AC 837 ms
31,780 KB
testcase_14 AC 819 ms
31,776 KB
testcase_15 AC 464 ms
10,352 KB
testcase_16 AC 687 ms
29,360 KB
testcase_17 AC 563 ms
18,488 KB
testcase_18 AC 512 ms
14,648 KB
testcase_19 AC 607 ms
24,744 KB
testcase_20 AC 942 ms
31,660 KB
testcase_21 AC 575 ms
19,644 KB
testcase_22 AC 840 ms
31,784 KB
testcase_23 AC 891 ms
31,784 KB
testcase_24 AC 797 ms
31,788 KB
testcase_25 AC 807 ms
31,780 KB
testcase_26 AC 833 ms
31,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.06.26 00:41:03
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;




struct LowestCommonAncestor {
private:
    struct edge {
        int to,cost;
        edge(int to, int cost) : to(to), cost(cost) {}
    };
    int n;
    int log;
    vector<vector<int>> parent;
    vector<int> dep;
    vector<vector<edge>> G;
    vector<ll> dis; 
    void dfs(const vector<vector<edge>> &G, int v, int p, int d, int di) {
        parent[0][v] = p;
        dep[v] = d;
        dis[v] = di;
        for (edge e : G[v]) {
            
            if (e.to != p) dfs(G, e.to, v, d + 1,di + e.cost);
        }
    }
public:
    LowestCommonAncestor(int n) : n(n) {
        G.resize(n);
    }
    void add_edge(int from, int to, int cost) {
        G[from].push_back(edge(to,cost));
        G[to].push_back(edge(from,cost));
    }
    
    void build(int root = 0) {
        log = log2(n) + 1;
        parent.resize(log, vector<int>(n));
        dep.resize(n);
        dis.resize(n);
        dfs(G, root, -1, 0, 0);
        for (int k = 0; k + 1 < log; k++) {
            for (int v = 0; v < G.size(); v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                }
                else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    int depth(int v) {
        return dep[v];
    }
    
    int lca(int u, int v) {
        if (dep[u] > dep[v]) swap(u, v);
        for (int k = 0; k < log; k++) if ((dep[v] - dep[u]) >> k & 1) v = parent[k][v];
        if (u == v) return u;
        for (int k = log - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int dist(int u, int v) {
        
        return dis[u] + dis[v] - 2 * dis[lca(u, v)];
    }
};
int main() {
    int n;cin >> n;
    LowestCommonAncestor lca(n);
    for (int i = 0; i < n-1; i++) {
        int a,b,c;cin >> a >> b >> c;a--;b--;
        lca.add_edge(a,b,c);
    }
    lca.build();
    int q;cin >> q;
    for (int i = 0; i < q; i++) {
        int s,t;cin >> s >> t;
        s--;t--;
        cout << lca.dist(s,t) << endl;
    }
    return 0;
}
0