結果

問題 No.386 貪欲な領主
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-07-02 13:01:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 2,852 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 171,608 KB
実行使用メモリ 32,304 KB
最終ジャッジ日時 2024-04-20 21:30:45
合計ジャッジ時間 3,807 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 225 ms
32,304 KB
testcase_05 AC 173 ms
27,696 KB
testcase_06 AC 175 ms
27,568 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 21 ms
5,944 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 184 ms
27,444 KB
testcase_15 AC 197 ms
32,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

class LCA {
private:
    static const int MAX_LOG = 20;
    const int n;
    Graph G;
    VV par;
    V depth;
    V cost;
    V U;
public:
    LCA(int _n) : n(_n), G(_n), par(MAX_LOG, V(_n)), depth(_n), cost(_n), U(_n) {}
    // undirected
    void addEdge(int a, int b) {
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    void readU() {
        rep(i, n) scanf("%lld", &U[i]);
    }
    // 各頂点の深さと、1つ前の親を求める
    void dfs(int v, int p, int d, int c) {
        par[0][v] = p;
        depth[v] = d;
        cost[v] = c + U[v];
        for (auto nxt : G[v]) {
            if (nxt != p) {
                dfs(nxt, v, d + 1, c + U[v]);
            }
        }
    }
    // 各頂点の2^k番目の親を求めておく
    void setPar() {
        // 0を根として1つ目の親を求める
        dfs(0, -1, 0, 0);

        // 2^i番目の親を求める
        rep(i, MAX_LOG - 1) {
            rep(j, n) {
                if (par[i][j] == -1) {
                    par[i + 1][j] = -1;
                } else {
                    par[i + 1][j] = par[i][par[i][j]];
                }
            }
        }
    }
    int lca(int a, int b) {
        // まずaとbの深さを揃える
        if (depth[a] > depth[b]) {
            swap(a, b);
        }
        rep(i, MAX_LOG) {
            if ((depth[b] - depth[a]) >> i & 1) {
                b = par[i][b];
            }
        }
        if (a == b) return a;

        // ぶつかる直前までa, bを上げる
        rrep(i, MAX_LOG) {
            if (par[i][a] != par[i][b]) {
                a = par[i][a];
                b = par[i][b];
            }
        }
        // aとbの1つ前の親は一致している
        return par[0][a];
    }
    int query(int a, int b) {
        int c = lca(a, b);
        return cost[a] + cost[b] - 2 * cost[c] + U[c];
    }
};

signed main() {
    int n;
    scanf("%lld", &n);
    LCA lca(n);
    rep(i, n - 1) {
        int a, b;
        scanf("%lld%lld", &a, &b);
        lca.addEdge(a, b);
    }
    lca.readU();
    lca.setPar();

    int Q;
    scanf("%lld", &Q);
    int ans = 0;
    rep(i, Q) {
        int a, b, m;
        scanf("%lld%lld%lld", &a, &b, &m);
        ans += m * lca.query(a, b);
    }
    printf("%lld\n", ans);
}
0