結果

問題 No.386 貪欲な領主
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-07-02 00:12:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 2,665 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 169,220 KB
実行使用メモリ 34,688 KB
最終ジャッジ日時 2024-04-20 21:07:05
合計ジャッジ時間 3,774 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 205 ms
34,560 KB
testcase_05 AC 274 ms
27,648 KB
testcase_06 AC 257 ms
27,392 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 26 ms
6,136 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 279 ms
27,520 KB
testcase_15 AC 192 ms
34,688 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;

const int MAX_N = 100010;
const int MAX_LOG = 20;
int N;

// par[i][j] = jの2^i番目の親(根を通り過ぎる場合は-1)
int par[MAX_LOG][MAX_N];
int depth[MAX_N];
int cost[MAX_N];
Graph G;
V U;

// 各頂点の深さと、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];
}

signed main() {
    scanf("%lld", &N);
    G.resize(N);
    rep(i, N - 1) {
        int a, b;
        scanf("%lld%lld", &a, &b);
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    U.resize(N);
    rep(i, N) scanf("%lld", &U[i]);

    setPar();

    // rep(i, N) {
    //     printf("%lld ", cost[i]);
    // }
    // printf("\n");

    int Q;
    scanf("%lld", &Q);
    int ans = 0;
    rep(i, Q) {
        int a, b, m;
        scanf("%lld%lld%lld", &a, &b, &m);
        int c = lca(a, b);
        ans += (cost[a] + cost[b] - 2 * cost[c] + U[c]) * m;

        // printf("%lld\n", (cost[a] + cost[b] - 2 * cost[c] + U[c]) * m);
    }
    printf("%lld\n", ans);
}
0