結果

問題 No.386 貪欲な領主
ユーザー けーむけーむ
提出日時 2020-08-21 18:28:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 2,726 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 178,548 KB
実行使用メモリ 22,400 KB
最終ジャッジ日時 2024-04-23 05:13:52
合計ジャッジ時間 4,363 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 257 ms
22,272 KB
testcase_05 AC 150 ms
18,560 KB
testcase_06 AC 161 ms
18,432 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 18 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 158 ms
18,432 KB
testcase_15 AC 205 ms
22,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }

template<class T>
struct LowestCommonAncestor {
private:
    const int LOG;
    vector<int> dep;
    const vector<vector<T>> &g;
    vector<vector<int>> table;
    
    void dfs(int idx, int par, int d) {
        table[0][idx] = par;
        dep[idx] = d;
        for(auto &to : g[idx]) {
            if(to != par) dfs(to, idx, d + 1);
        }
    }
    
public:
    LowestCommonAncestor(const vector<vector<T>> &g, int root) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
        table.assign(LOG, vector<int>(g.size(), -1));
        dfs(root, -1, 0);
        for(int k = 0; k + 1 < LOG; k++) {
            for(int i = 0; i < table[k].size(); i++) {
                if(table[k][i] == -1) table[k + 1][i] = -1;
                else table[k + 1][i] = table[k][table[k][i]];
            }
        }
    }
    
    int query(int u, int v) {
        if(dep[u] > dep[v]) swap(u, v);
        for(int i = LOG - 1; i >= 0; i--) {
            if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
        }
        if(u == v) return u;
        for(int i = LOG - 1; i >= 0; i--) {
            if(table[i][u] != table[i][v]) {
                u = table[i][u];
                v = table[i][v];
            }
        }
        return table[0][u];
    }
};

ll n;
vector<vector<ll>> g;
vector<ll> u, dist;

void dfs(ll v, ll p = -1) {
    for(auto &x : g[v]) {
        if (x == p) continue;
        dist[x] = dist[v] + u[x];
        dfs(x, v);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    cin >> n;
    g.resize(n);
    rep(i, n - 1) {
        ll a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    u.resize(n);
    rep(i, n) cin >> u[i];
    dist.resize(n);
    dist[0] = u[0];
    dfs(0);
    LowestCommonAncestor<ll> lca(g, 0);
    ll m;
    cin >> m;
    ll ans = 0;
    rep(i, m) {
        ll a, b, c;
        cin >> a >> b >> c;
        ll p = lca.query(a, b);
        ll d = dist[a] + dist[b] - 2 * dist[p] + u[p];
        ans += d * c;
    }
    cout << ans << endl;
    return 0;
}
0