結果

問題 No.1094 木登り / Climbing tree
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2021-01-22 21:37:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,214 bytes
コンパイル時間 1,458 ms
コンパイル使用メモリ 94,100 KB
実行使用メモリ 814,136 KB
最終ジャッジ日時 2023-08-27 17:56:07
合計ジャッジ時間 5,524 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 MLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include<iostream>
#include<cstdint>
#include<set>
#include<cstddef>
#include<vector>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = size_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
template<typename T>
using vec = vector<T>;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define len(hoge) (i64)((hoge).size())
using P = pair<i64, i64>;

template<typename T>
class LCA {
public:
    vector<int_fast32_t> depth;
    const int_fast32_t dig;
    vector<vector<int_fast32_t>>g;
    vector<vector<int_fast64_t>> cost;
    vector<int_fast64_t> costs;
    vector<vector<int_fast32_t>> doubling;

    LCA(int_fast64_t n) : depth(n), costs(n), cost(n), g(n),dig(n) {
        doubling.assign(dig, vector<int_fast32_t>(n, -1));
    }

    void addedge(int_fast32_t u, int_fast32_t v, int_fast64_t c = 0) {
        g[u].emplace_back(v);
        cost[u].emplace_back(c);
        g[v].emplace_back(u);
        cost[v].emplace_back(c);

    }

    void dfs(int_fast32_t now, int_fast32_t par = -1, int_fast32_t d = 0, int_fast64_t c = 0) {
        doubling[0][now] = par;
        depth[now] = d;
        costs[now] = c;
        rep(i,len(g[now])) {
            if (g[now][i] != par)dfs(g[now][i], now, d + 1, c + cost[now][i]);
        }
    }

    void construct() {
        dfs(0);
        for (int_fast32_t i = 0; i < dig - 1; i++) {
            for (int_fast32_t j = 0; j < doubling[i].size(); j++) {
                if (doubling[i][j] == -1)doubling[i + 1][j] = -1;
                else doubling[i + 1][j] = doubling[i][doubling[i][j]];
            }
        }
    }

    int_fast32_t query_answer(int_fast32_t u, int_fast32_t v) {
        if (depth[u] > depth[v])swap(u, v);
        for (int_fast32_t i = dig - 1; 0 <= i; i--) {
            if (((depth[v] - depth[u]) >> i) & 1) v = doubling[i][v];
        }
        if (u == v) return u;
        for (int_fast32_t i = dig - 1; 0 <= i; --i) {
            if (doubling[i][u] != doubling[i][v]) {
                u = doubling[i][u];
                v = doubling[i][v];
            }
        }
        return doubling[0][u];
    }
    int_fast32_t length(int_fast32_t u,int_fast32_t v){
        return depth[u] + depth[v] - 2 * depth[query_answer(u,v)];
    }
    int_fast64_t dist(int_fast32_t u,int_fast32_t v){
        return costs[u] + costs[v] - 2 * costs[query_answer(u,v)];
    }
    bool isOnPath(int_fast32_t u, int_fast32_t v,int_fast32_t x){
        return length(u,x) + length(v,x) == length(u,v);
    }
};

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    i32 n, q;
    cin >> n;
    LCA<i64> lca(n);
    rep(i, n - 1) {
        i32 a, b;
        i64 c;
        cin >> a >> b >> c;
        a--, b--;
        lca.addedge(a,b,c);
    }
    lca.construct();
    cin >> q;
    rep(i, q) {
        i32 s, t;
        cin >> s >> t;
        cout << lca.dist(s - 1,t - 1) << endl;
    }
}
0