結果

問題 No.898 tri-βutree
ユーザー Y17Y17
提出日時 2019-10-04 21:45:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 585 ms / 4,000 ms
コード長 2,562 bytes
コンパイル時間 2,139 ms
コンパイル使用メモリ 172,140 KB
実行使用メモリ 37,760 KB
最終ジャッジ日時 2024-04-26 08:31:54
合計ジャッジ時間 14,472 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 339 ms
37,760 KB
testcase_01 AC 17 ms
22,784 KB
testcase_02 AC 18 ms
22,784 KB
testcase_03 AC 18 ms
22,912 KB
testcase_04 AC 17 ms
22,796 KB
testcase_05 AC 17 ms
22,792 KB
testcase_06 AC 17 ms
22,656 KB
testcase_07 AC 579 ms
30,664 KB
testcase_08 AC 580 ms
30,664 KB
testcase_09 AC 570 ms
30,528 KB
testcase_10 AC 569 ms
30,848 KB
testcase_11 AC 577 ms
30,604 KB
testcase_12 AC 581 ms
30,592 KB
testcase_13 AC 579 ms
30,660 KB
testcase_14 AC 582 ms
30,656 KB
testcase_15 AC 585 ms
30,720 KB
testcase_16 AC 577 ms
30,592 KB
testcase_17 AC 573 ms
30,660 KB
testcase_18 AC 572 ms
30,664 KB
testcase_19 AC 570 ms
30,676 KB
testcase_20 AC 574 ms
30,656 KB
testcase_21 AC 571 ms
30,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long

//最小共通祖先

//コンストラクタ(g) : 木Gで初期化
//build() : 構築する
//query(v, u): 頂点vとuの最小共通祖先を返す

// G : vector<vector<int>>
//木は根付き木でなくてもよい

template< typename G >
struct DoublingLowestCommonAncestor {
    const int LOG;
    vector< int > dep;
    const G &g;
    vector< vector< int > > table;

    DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
        table.assign(LOG, vector< int >(g.size(), -1));
    }

    void dfs(int idx, int par, int d) {
        table[0][idx] = par;
        dep[idx] = d;
        for(auto &to : g[idx]) {
            if(to != par) dfs(to, idx, d + 1);
        }
    }

    void build() {
        dfs(0, -1, 0);
        for(int k = 0; k + 1 < LOG; k++) {
            for(int i = 0; i < table[k].size(); i++) {
                if(table[k][i] == -1) table[k + 1][i] = -1;
                else table[k + 1][i] = table[k][table[k][i]];
            }
        }
    }

    int query(int u, int v) {
        if(dep[u] > dep[v]) swap(u, v);
        for(int i = LOG - 1; i >= 0; i--) {
            if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
        }
        if(u == v) return u;
        for(int i = LOG - 1; i >= 0; i--) {
            if(table[i][u] != table[i][v]) {
                u = table[i][u];
                v = table[i][v];
            }
        }
        return table[0][u];
    }
};

vector<pair<int, int> > graph[100010];
bool used[100010];

vector<vector<int> > tree(100010);
int deep[100010];

void dfs(int idx, int d){
    used[idx] = true;
    deep[idx] = d;

    for(int i = 0;i < graph[idx].size();i++){
        if(!used[graph[idx][i].first]){
            tree[idx].push_back(graph[idx][i].first);
            dfs(graph[idx][i].first, graph[idx][i].second + d);
        }
    }
    return;
}


signed main(){
    int n;
    cin >> n;

    for(int i = 0;i < n-1;i++){
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back({v, w});
        graph[v].push_back({u, w});
    }

    dfs(0, 0);

    DoublingLowestCommonAncestor<vector<vector<int>> > lca(tree);
    lca.build();

    auto get = [&](int a, int b){
        return deep[a] + deep[b] - deep[lca.query(a, b)] * 2;
    };

    int q;
    cin >> q;

    for(int i = 0;i < q;i++){
        int a, b, c;
        cin >> a >> b >> c;
        int ans = get(a, b) + get(b, c) + get(c, a);

        cout << ans / 2 << endl;
    }

    return 0;
}
0