結果

問題 No.898 tri-βutree
ユーザー treeonetreeone
提出日時 2019-10-04 23:30:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 829 ms / 4,000 ms
コード長 2,408 bytes
コンパイル時間 3,020 ms
コンパイル使用メモリ 210,608 KB
実行使用メモリ 60,632 KB
最終ジャッジ日時 2024-04-26 08:45:03
合計ジャッジ時間 17,861 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 275 ms
60,632 KB
testcase_01 AC 3 ms
6,016 KB
testcase_02 AC 4 ms
6,144 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 5 ms
5,888 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 4 ms
6,016 KB
testcase_07 AC 806 ms
57,556 KB
testcase_08 AC 811 ms
57,556 KB
testcase_09 AC 809 ms
57,528 KB
testcase_10 AC 818 ms
57,560 KB
testcase_11 AC 829 ms
57,556 KB
testcase_12 AC 814 ms
57,556 KB
testcase_13 AC 802 ms
57,680 KB
testcase_14 AC 812 ms
57,632 KB
testcase_15 AC 807 ms
57,628 KB
testcase_16 AC 802 ms
57,684 KB
testcase_17 AC 796 ms
57,560 KB
testcase_18 AC 823 ms
57,556 KB
testcase_19 AC 820 ms
57,544 KB
testcase_20 AC 828 ms
57,556 KB
testcase_21 AC 810 ms
57,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

struct LowestCommonAncestor{
    const int MAX_LOG_V = 50;
    vector<vector<int> > G, parent;
    int root = 0, V;
    vector<int> depth;
    LowestCommonAncestor(int _V){
        V = _V;
        G.resize(V);
        parent.resize(MAX_LOG_V, vector<int>(V));
        depth.resize(V);
    }
    void add_edge(int u, int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    void dfs(int v, int p, int d){
        parent[0][v] = p;
        depth[v] = d;
        for(int i = 0; i < G[v].size(); i++){
            if(G[v][i] != p) dfs(G[v][i], v, d + 1);
        }
    }
    void build(){
        dfs(root, -1, 0);
        for(int k = 0; k + 1 < MAX_LOG_V; k++){
            for(int v = 0; v < V; v++){
                if(parent[k][v] < 0) parent[k + 1][v] = -1;
                else parent[k + 1][v] = parent[k][parent[k][v]];
            }
        }
    }
    int query(int u, int v){
        if(depth[u] > depth[v]) swap(u, v);
        for(int k = 0; k < MAX_LOG_V; k++){
            if((depth[v] - depth[u]) >> k & 1){
                v = parent[k][v];
            }
        }
        if(u == v) return u;
        for(int k = MAX_LOG_V - 1; k >= 0; k--){
            if(parent[k][u] != parent[k][v]){
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];    
    }
};

struct edge{
   int v, w;
};

vector<edge> G[100010];
int sum[100010];

void dfs(int v, int p, int now){
    sum[v] = now;
    for(auto i : G[v]){
        if(i.v == p) continue;
        dfs(i.v, v, now + i.w);
    }
}

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    LowestCommonAncestor lca(n);
    rep(i, 0, n - 1){
        int u, v, w;
        cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
        lca.add_edge(u, v);
    }
    dfs(0, -1, 0);
    lca.build();
    int q;
    cin >> q;
    while(q--){
        int x, y, z;
        cin >> x >> y >> z;
        auto f = [](int u, int v, int w){ return sum[u] + sum[v] - 2 * sum[w]; };
        int ans = (f(x, y, lca.query(x, y)) + f(y, z, lca.query(y, z)) + f(z, x, lca.query(z, x))) / 2;
        cout << ans << endl;
    }
}
0