結果

問題 No.386 貪欲な領主
ユーザー しらっ亭しらっ亭
提出日時 2016-07-02 00:07:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 3,063 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 177,228 KB
実行使用メモリ 22,648 KB
最終ジャッジ日時 2023-08-02 21:03:46
合計ジャッジ時間 3,821 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 191 ms
22,648 KB
testcase_05 AC 140 ms
17,200 KB
testcase_06 AC 152 ms
16,864 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 17 ms
4,404 KB
testcase_09 AC 3 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 151 ms
16,808 KB
testcase_15 AC 167 ms
22,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL<<(n))
#define SZ(x) ((int)(x).size())
#define fst first
#define snd second
typedef vector<int> vi;typedef vector<vi> vvi;typedef pair<int,int> pii;typedef vector<pii> vpii;
typedef long long ll;

class LCA {
  public:
    int H, W;
    vector<vector<int> > parent;
    vector<int> depth;

    // LCA(木の頂点数)
    LCA(int V, vector<vector<int> >& E) : W(V), depth(V) {
      H = 1;
      while (1 << H < W) H++;
      parent.resize(H, vector<int>(W));
      // 適当な頂点からdfsして親を求めておく
      dfs(E, 0, -1, 0);
      // 親をダブリングする
      for (int k = 1; k < H; k++) {
        for (int v = 0; v < W; v++) {
          parent[k][v] = parent[k-1][v] == -1 ? -1 : parent[k-1][parent[k-1][v]];
        }
      }
    }

    void dfs(vector<vector<int> >& E, int cur, int par, int dep) {
      parent[0][cur] = par;
      depth[cur] = dep;
      for (int i = 0; i < (int) E[cur].size(); i++) {
        if (E[cur][i] != par) dfs(E, E[cur][i], cur, dep + 1);
      }
    }

    // u と v の LCA を求める
    int query(int u, int v) {
      // 深い方を v にしておく
      if (depth[u] > depth[v]) swap(u, v);
      // 根からの深さが u と同じになるまで v の親をたどる
      for (int k = 0; k < H; k++) {
        if ((depth[v]-depth[u]) >> k & 1) {
          v = parent[k][v];
        }
      }
      if (u == v) return u;
      // 二分探索でLCAを求める
      for (int k = H - 1; k >= 0; k--) {
        if (parent[k][u] != parent[k][v]) {
          u = parent[k][u];
          v = parent[k][v];
        }
      }
      return parent[0][u];
    }
};

vi U;
vvi E;
vi tax;

void dfs(int cur, int pre, int ct) {
  tax[cur] = ct + U[cur];
  forr(nex, E[cur]) {
    if (nex != pre) {
      dfs(nex, cur, ct + U[cur]);
    }
  }
}

void Main() {
  int N;
  scanf("%d", &N);
  E.resize(N);
  tax.resize(N);
  rep(i,N-1) {
    int a, b;
    scanf("%d%d", &a, &b);
    E[a].push_back(b);
    E[b].push_back(a);
  }

  U.resize(N);
  rep(i, N) scanf("%d", &U[i]);

  LCA lca(N, E);

  tax.resize(N);
  dfs(0, -1, 0);

  //rep(i,SZ(tax)) cout<<"tax["<<i<<"]: "<<tax[i]<<endl;

  ll ans = 0;

  int M;
  scanf("%d", &M);
  rep(i,M) {
    int a,b,c;
    scanf("%d%d%d", &a, &b, &c);
    int x = lca.query(a, b);

    ll tt = tax[a] + tax[b] - tax[x] * 2 + U[x];
    //_p("(%d,%d):%d, %lld(%d,%d,%d)\n", a,b,x,tt,tax[a],tax[b],tax[x]);

    ans += tt * c;
  }
  _p("%lld\n", ans);
}
int main() { cin.tie(0); ios::sync_with_stdio(false); Main(); return 0; }
0