結果

問題 No.386 貪欲な領主
ユーザー parukiparuki
提出日時 2016-07-02 00:41:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 2,703 bytes
コンパイル時間 2,045 ms
コンパイル使用メモリ 171,152 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-04-20 21:26:35
合計ジャッジ時間 3,712 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 187 ms
22,656 KB
testcase_05 AC 144 ms
18,048 KB
testcase_06 AC 143 ms
18,008 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 18 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 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 151 ms
18,044 KB
testcase_15 AC 158 ms
22,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

class LCA{
public:
    LCA(){ }
    LCA(const vector<vector<int>> &G, int root = 0):V((int)G.size()), depth(V), parent(V){
        rep(i, LG)ancestor[i] = vector<int>(V);
        dfs(root, 0, 0, G);
        rep(i, V)
            ancestor[0][i] = parent[i];
        rep(i,LG-1)rep(v,V)
            ancestor[i + 1][v] = ancestor[i][ancestor[i][v]];
    }
    int get(int u, int v){
        if(depth[u] < depth[v]) swap(u, v);
        int dist = depth[u] - depth[v];
        for(int i = 0; i < LG; ++i)
            if((dist >> i) & 1)
                u = ancestor[i][u];
        if(u == v) return u;
        for(int i = LG - 1; i >= 0; --i){
            if(ancestor[i][u] != ancestor[i][v]){
                u = ancestor[i][u];
                v = ancestor[i][v];
            }
        }
        return ancestor[0][u];
    }
    int getDepth(int u){
        return depth[u];
    }
    int goUp(int u, int len){
        for(int i = 0; i < LG; ++i)
            if(len >> i & 1)u = ancestor[i][u];
        return u;
    }
    int distance(int u, int v){
        return depth[u] + depth[v] - 2 * depth[get(u, v)];
    }
    int getParent(int u){
        return parent[u];
    }
private:
    static const int LG = 18;
    int V;
    vector<int> depth, parent, ancestor[LG];
    void dfs(int v, int p, int d, const vector<vector<int>> &G){
        depth[v] = d;
        parent[v] = p;
        for(int to : G[v]){
            if(to != p){
                dfs(to, v, d + 1, G);
            }
        }
    }
};

const int MAX = (int)100000;
int N, M, U[MAX];
ll sum[MAX];
vector<vi> G;

void dfs(int u, int pre){
    sum[u] = U[u];
    if(pre != -1)sum[u] += sum[pre];
    each(v, G[u])if(v != pre)dfs(v, u);
}

int main(){
    cin >> N;
    G.resize(N);
    rep(i, N - 1){
        int u, v; scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    rep(i, N)scanf("%d", U + i);
    auto lca = LCA(G);
    dfs(0, -1);
    cin >> M;
    ll ans = 0;
    while(M--){
        int A, B, C;
        scanf("%d%d%d", &A, &B, &C);
        ll l = lca.get(A, B), val = sum[A] + sum[B];
        val -= sum[l]*2;
        val += U[l];
        ans += val*C;
    }
    cout << ans << endl;
}
0