結果

問題 No.898 tri-βutree
ユーザー rogi52rogi52
提出日時 2022-10-10 20:01:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 4,000 ms
コード長 2,607 bytes
コンパイル時間 3,097 ms
コンパイル使用メモリ 216,128 KB
実行使用メモリ 31,248 KB
最終ジャッジ日時 2023-09-06 22:46:13
合計ジャッジ時間 11,245 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
31,248 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 230 ms
23,932 KB
testcase_08 AC 225 ms
24,012 KB
testcase_09 AC 224 ms
23,880 KB
testcase_10 AC 243 ms
24,020 KB
testcase_11 AC 228 ms
23,904 KB
testcase_12 AC 228 ms
23,924 KB
testcase_13 AC 233 ms
23,864 KB
testcase_14 AC 241 ms
23,896 KB
testcase_15 AC 222 ms
23,944 KB
testcase_16 AC 225 ms
24,016 KB
testcase_17 AC 234 ms
23,940 KB
testcase_18 AC 232 ms
23,952 KB
testcase_19 AC 228 ms
23,968 KB
testcase_20 AC 233 ms
23,956 KB
testcase_21 AC 239 ms
23,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

struct LCA {
    vector<vector<int>> parent; // parent[d][v] := 2^d-th parent of v
    vector<int> depth;
    LCA() { }
    LCA(const vector<vector<int>> &G, int r = 0){ init(G, r); }

    void init(const vector<vector<int>> &G, int r = 0) {
        int V = (int)G.size();
        int h = 1;
        while((1 << h) < V) ++h;
        parent.assign(h, vector<int> (V, -1));
        depth.assign(V, -1);
        dfs(G, r, -1, 0);
        for(int i = 0; i + 1 < (int)parent.size(); ++i){
            for(int v = 0; v < V; ++v) {
                if(parent[i][v] != -1){
                    parent[i + 1][v] = parent[i][parent[i][v]];
                }
            }
        }
    }

    void dfs(const vector<vector<int>> &G, int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for(auto e : G[v]) if(e != p) dfs(G, e, v, d + 1);
    }

    int query(int u, int v) {
        if(depth[u] > depth[v]) swap(u, v);
        for(int i = 0; i < (int)parent.size(); ++i) {
            if((depth[v] - depth[u]) & (1 << i)) v = parent[i][v];
        }
        if(u == v) return u;
        for(int i = (int)parent.size() - 1; i >= 0; --i) {
            if(parent[i][u] != parent[i][v]) {
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        return parent[0][u];
    }

    int dist(int u, int v) { 
        return depth[u] + depth[v] - 2 * depth[query(u, v)];
    }

    bool is_on_path(int u, int v, int x) {
        return dist(u, x) + dist(x, v) == dist(u, v);
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N; cin >> N;
    vector<vector<pair<int,ll>>> G(N);
    vector<vector<int>> H(N);
    rep(i,N-1) {
        int u,v,w; cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});
        H[u].push_back(v);
        H[v].push_back(u);
    }

    int ROOT = 0;
    vector<ll> sum(N, 0);
    function<void(int,int)> dfs = [&](int v, int p) -> void {
        for(auto [to, w] : G[v]) {
            if(to != p) {
                sum[to] = sum[v] + w;
                dfs(to, v);
            }
        }
    }; dfs(ROOT, -1);

    LCA lca(H, ROOT);
    int Q; cin >> Q;
    rep(_,Q) {
        int x,y,z; cin >> x >> y >> z;
        ll ans = 0;
        ans += sum[x] + sum[y] - 2 * sum[lca.query(x, y)];
        ans += sum[y] + sum[z] - 2 * sum[lca.query(y, z)];
        ans += sum[z] + sum[x] - 2 * sum[lca.query(z, x)];
        ans /= 2;
        cout << ans << "\n";
    }
}
0