結果

問題 No.386 貪欲な領主
ユーザー kyunakyuna
提出日時 2019-08-21 01:24:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 1,075 ms
コンパイル使用メモリ 83,320 KB
実行使用メモリ 23,604 KB
最終ジャッジ日時 2024-04-16 04:29:55
合計ジャッジ時間 4,419 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 417 ms
23,604 KB
testcase_05 AC 310 ms
17,672 KB
testcase_06 AC 323 ms
17,448 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 37 ms
5,376 KB
testcase_09 AC 6 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 9 ms
5,376 KB
testcase_14 AC 323 ms
17,468 KB
testcase_15 AC 361 ms
23,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

using UnWeightedGraph = vector<vector<int>>;
struct LowestCommonAncestor {
    const UnWeightedGraph &g;
    int root;
    vector<vector<int>> parent;         // parent[k][v] := 2^k-th parent of v
    vector<int> depth;
    LowestCommonAncestor(const UnWeightedGraph &g, int r = 0) : \
        g(g), root(r), depth(g.size()) { }
    void build() {
        int V = g.size();
        int h = 1; while ((1 << h) < V) ++h;    // 32 - __builtin_clz(V)
        parent.assign(h, vector<int>(V, -1));
        dfs(root, -1, 0);
        for (int k = 0; k + 1 < h; ++k) for (int v = 0; v < V; ++v) {
            if (parent[k][v] != -1) parent[k + 1][v] = parent[k][parent[k][v]];
        }
    }
    void dfs(int u, int p, int d) {
        parent[0][u] = p;
        depth[u] = d;
        for (auto v: g[u]) if (v != p) dfs(v, u, d + 1);
    }
    int get(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (int k = 0; k < parent.size(); ++k) {
            if ((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
        }
        if (u == v) return u;
        for (int k = parent.size() - 1; k >= 0; --k) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u]; v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int dist(int u, int v) { return depth[u] + depth[v] - depth[get(u, v)] * 2; }
};

vector<long long> cost, acc;
void rec(const UnWeightedGraph &g, int u, int p, int c) {
    acc[u] = c + cost[u];
    for (auto v: g[u]) if (v != p) {
        rec(g, v, u, acc[u]);
    }
}

int main() {
    int N; cin >> N;
    UnWeightedGraph g(N);
    for (int i = 0; i < N - 1; i++) {
        int a, b; cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    LowestCommonAncestor lca(g);
    lca.build();
    cost.resize(N); acc.resize(N);
    for (auto &ci: cost) cin >> ci;
    rec(g, 0, -1, 0);
    long long res = 0;
    int Q; cin >> Q;
    while (Q--) {
        int a, b, c; cin >> a >> b >> c;
        int p = lca.get(a, b);
        res += (acc[a] + acc[b] - acc[p] * 2 + cost[p]) * c;
    }
    cout << res << endl;
    return 0;
}
0