結果

問題 No.1094 木登り / Climbing tree
ユーザー MisterMister
提出日時 2020-08-02 14:25:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 3,296 bytes
コンパイル時間 1,385 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 56,992 KB
最終ジャッジ日時 2024-04-25 19:21:18
合計ジャッジ時間 12,040 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 342 ms
53,244 KB
testcase_02 AC 143 ms
56,992 KB
testcase_03 AC 47 ms
6,940 KB
testcase_04 AC 95 ms
23,680 KB
testcase_05 AC 201 ms
46,736 KB
testcase_06 AC 142 ms
18,048 KB
testcase_07 AC 381 ms
53,380 KB
testcase_08 AC 360 ms
53,176 KB
testcase_09 AC 360 ms
53,368 KB
testcase_10 AC 369 ms
53,308 KB
testcase_11 AC 359 ms
53,396 KB
testcase_12 AC 352 ms
53,288 KB
testcase_13 AC 352 ms
53,404 KB
testcase_14 AC 384 ms
53,372 KB
testcase_15 AC 145 ms
14,336 KB
testcase_16 AC 289 ms
44,228 KB
testcase_17 AC 212 ms
26,112 KB
testcase_18 AC 177 ms
20,224 KB
testcase_19 AC 231 ms
35,412 KB
testcase_20 AC 366 ms
53,428 KB
testcase_21 AC 214 ms
27,776 KB
testcase_22 AC 363 ms
53,368 KB
testcase_23 AC 354 ms
53,320 KB
testcase_24 AC 358 ms
53,332 KB
testcase_25 AC 363 ms
53,492 KB
testcase_26 AC 361 ms
53,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class Cost = int>
struct LevelAncestor {
    Graph<Cost> tree;
    std::vector<std::vector<int>> par;
    std::vector<int> depth;
    std::vector<Cost> cdepth;
    int kmax;

    void dfs(int v, int p = -1, int d = 0, Cost c = 0) {
        par[v][0] = p;
        depth[v] = d;
        cdepth[v] = c;

        for (const auto& e : tree[v]) {
            if (e.dst == p) continue;
            dfs(e.dst, v, d + 1, c + e.cost);
        }
    }

    LevelAncestor(const Graph<Cost>& tree, int root)
        : tree(tree), par(tree.size()), depth(tree.size(), -1), cdepth(tree.size()) {
        kmax = 0;
        while ((1 << kmax) < (int)tree.size()) ++kmax;
        for (auto& v : par) v.resize(kmax + 1);

        dfs(root);

        for (int k = 1; k <= kmax; ++k) {
            for (int v = 0; v < tree.size(); ++v) {
                int p = par[v][k - 1];
                par[v][k] = (p == -1 ? -1 : par[p][k - 1]);
            }
        }
    }

    int climb(int v, int d) const {
        for (int k = kmax; k >= 0 && v != -1; --k) {
            if ((1 << k) > d) continue;

            v = par[v][k];
            d -= (1 << k);
        }
        return v;
    }

    int lca(int u, int v) const {
        if (depth[u] < depth[v]) std::swap(u, v);

        if (depth[u] > depth[v]) {
            u = climb(u, depth[u] - depth[v]);
        }

        if (u == v) return u;

        for (int k = kmax; k >= 0; --k) {
            if (par[u][k] != par[v][k]) {
                u = par[u][k];
                v = par[v][k];
            }
        }
        return par[u][0];
    }

    int dist(int u, int v) const {
        int p = lca(u, v);
        return depth[u] + depth[v] - depth[p] * 2;
    }

    Cost cdist(int u, int v) const {
        int p = lca(u, v);
        return cdepth[u] + cdepth[v] - cdepth[p] * 2;
    }
};

void solve() {
    int n;
    std::cin >> n;

    Graph<int> graph(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v, c;
        std::cin >> u >> v >> c;
        graph.span(false, --u, --v, c);
    }

    LevelAncestor<int> la(graph, 0);

    int q;
    std::cin >> q;
    while (q--) {
        int u, v;
        std::cin >> u >> v;
        std::cout << la.cdist(--u, --v) << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0