結果

問題 No.386 貪欲な領主
ユーザー oshiborioshibori
提出日時 2018-02-15 22:52:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 3,038 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 182,556 KB
実行使用メモリ 42,656 KB
最終ジャッジ日時 2023-08-28 15:29:58
合計ジャッジ時間 4,776 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 283 ms
42,656 KB
testcase_05 AC 196 ms
40,204 KB
testcase_06 AC 218 ms
39,892 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 21 ms
6,700 KB
testcase_09 AC 4 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 6 ms
4,452 KB
testcase_14 AC 209 ms
39,780 KB
testcase_15 AC 235 ms
42,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
// typedef __int128_t Int;
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct Graph {
  int V;
  vector<vector<int>> g;
  vector<int> dist;
  Graph(int V) : V(V), g(V), dist(V, -1) {}
  void add_edge(int s, int t) {
    g[s].push_back(t);
    g[t].push_back(s);
  }
};

struct Tree { // rooted tree
  vector<vector<int>> g, parent;
  int root, V, log2_n;
  vector<int> depth, U, cost;

  int get_depth(int x) { return depth[x]; }
  void dfs(int v, int p, int d) {
    parent[0][v] = p;
    depth[v] = d;
    rep(i, 0, g[v].size()) {
      if (g[v][i] != p)
        dfs(g[v][i], v, d + 1);
    }
  }
  void bfs() {
    queue<int> q;
    q.push(root);
    cost[root] = U[root];
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (auto u : g[v]) {
        if (cost[u])
          continue;
        cost[u] = cost[v] + U[u];
        q.push(u);
      }
    }
  }
  Tree(Graph G, int root, vector<int> U)
      : root(root), V(G.V), g(G.g), depth(V), log2_n(1 + (int)log2(V)), U(U) {
    parent.resize(log2_n, vector<int>(V));
    cost.resize(V);
    dfs(root, -1, 0);
    for (int k = 0; k + 1 < log2_n; k++) {
      for (int v = 0; v < V; v++) {
        if (parent[k][v] < 0) {
          parent[k + 1][v] = -1;
        }

        else {
          parent[k + 1][v] = parent[k][parent[k][v]];
        }
      }
    }
    bfs();
  }
  int lca(int u, int v) {
    int ret = 0;
    if (depth[u] > depth[v])
      swap(u, v);
    for (int k = 0; k < log2_n; k++) {
      if ((depth[v] - depth[u]) >> k & 1) {
        v = parent[k][v];
      }
    }
    if (u == v)
      return u;
    for (int k = log2_n - 1; k >= 0; k--) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }
    return parent[0][u];
  }
  int query(int a, int b) {
    int u = lca(a, b);
    return cost[a] + cost[b] - cost[u] * 2 + U[u];
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  Graph g(N);
  loop(N - 1) {
    int a, b;
    cin >> a >> b;
    g.add_edge(a, b);
  }
  vector<int> U(N);
  rep(i, 0, N) { cin >> U[i]; }
  Tree tree(g, 0, U);
  int M;
  cin >> M;
  int ans = 0;
  loop(M) {
    int a, b, c;
    cin >> a >> b >> c;
    ans += tree.query(a, b)*c;
  }
  cout << ans << endl;

  return 0;
}
0