結果

問題 No.1094 木登り / Climbing tree
ユーザー uw_yu1rabbituw_yu1rabbit
提出日時 2021-01-22 21:46:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,172 ms / 2,000 ms
コード長 3,310 bytes
コンパイル時間 1,407 ms
コンパイル使用メモリ 95,420 KB
実行使用メモリ 140,424 KB
最終ジャッジ日時 2024-04-27 05:03:02
合計ジャッジ時間 28,284 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1,135 ms
130,352 KB
testcase_02 AC 191 ms
140,424 KB
testcase_03 AC 192 ms
9,600 KB
testcase_04 AC 259 ms
58,592 KB
testcase_05 AC 327 ms
113,464 KB
testcase_06 AC 651 ms
43,364 KB
testcase_07 AC 1,121 ms
130,392 KB
testcase_08 AC 1,154 ms
130,552 KB
testcase_09 AC 1,136 ms
130,492 KB
testcase_10 AC 1,141 ms
130,388 KB
testcase_11 AC 1,139 ms
130,380 KB
testcase_12 AC 1,145 ms
130,376 KB
testcase_13 AC 1,132 ms
130,416 KB
testcase_14 AC 1,126 ms
130,384 KB
testcase_15 AC 406 ms
33,384 KB
testcase_16 AC 626 ms
107,476 KB
testcase_17 AC 494 ms
65,248 KB
testcase_18 AC 479 ms
49,504 KB
testcase_19 AC 618 ms
90,484 KB
testcase_20 AC 1,159 ms
130,400 KB
testcase_21 AC 560 ms
69,732 KB
testcase_22 AC 1,151 ms
130,352 KB
testcase_23 AC 1,172 ms
130,392 KB
testcase_24 AC 1,158 ms
130,316 KB
testcase_25 AC 1,129 ms
130,220 KB
testcase_26 AC 1,149 ms
130,356 KB
権限があれば一括ダウンロードができます

ソースコード

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_fast32_t>> cost;
    vector<int_fast32_t> costs;
    int_fast32_t n;
    vector<vector<int_fast32_t>> doubling;

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

    void addedge(int_fast32_t u, int_fast32_t v, int_fast32_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_fast32_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_fast32_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, 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