結果

問題 No.898 tri-βutree
ユーザー iqueue02iqueue02
提出日時 2019-12-31 21:48:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 4,000 ms
コード長 2,078 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 181,228 KB
実行使用メモリ 20,752 KB
最終ジャッジ日時 2024-04-26 09:08:22
合計ジャッジ時間 7,080 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
20,752 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 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 190 ms
17,424 KB
testcase_08 AC 195 ms
17,424 KB
testcase_09 AC 183 ms
17,296 KB
testcase_10 AC 178 ms
17,424 KB
testcase_11 AC 190 ms
17,296 KB
testcase_12 AC 176 ms
17,424 KB
testcase_13 AC 174 ms
17,556 KB
testcase_14 AC 176 ms
17,540 KB
testcase_15 AC 169 ms
17,300 KB
testcase_16 AC 185 ms
17,300 KB
testcase_17 AC 201 ms
17,444 KB
testcase_18 AC 189 ms
17,424 KB
testcase_19 AC 178 ms
17,552 KB
testcase_20 AC 185 ms
17,292 KB
testcase_21 AC 182 ms
17,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

struct LowestCommonAncestor {
  int n, h;
  vector<vector<int>> par;
  vector<vector<pair<int,int>>> g;
  vector<int> dep;
  vector<ll> weight;

  LowestCommonAncestor(int sz) {
    n = sz; h = 1;
    g.resize(n), dep.resize(n), weight.resize(n);
    while ((1 << h) <= n) h++;
    par.assign(h, vector<int>(n, -1));
  }

  void add_edge(int u, int v, int w = 0) {
    g[u].emplace_back(make_pair(v, w));
    g[v].emplace_back(make_pair(u, w));
  }

  void dfs(int u, int p, int depth) {
    par[0][u] = p;
    dep[u] = depth;
    if (p == -1) weight[u] = 0;
    for (auto v : g[u]) {
      if (v.first == p) continue;
      weight[v.first] = weight[u] + v.second;
      dfs(v.first, u, depth + 1);
    }
  }

  void build(int root = 0) {
    dfs(root, -1, 0);
    for (int i = 0; i + 1 < h; i++) {
      for (int j = 0; j < n; j++) {
        if (par[i][j] != -1) par[i+1][j] = par[i][par[i][j]];
        else par[i+1][j] = -1;
      }
    }
  }

  int query(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    for (int i = h-1; i >= 0; i--) {
      if ( ((dep[v] - dep[u]) >> i) & 1) v = par[i][v];
    }
    if (u == v) return u;
    for (int i = h-1; i >= 0; i--) {
      if (par[i][u] != par[i][v]) {
        u = par[i][u];
        v = par[i][v];
      }
    }
    return par[0][u];
  }

  int dist(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[query(u, v)];
  }

  ll weightedsum(int u, int v) {
    return weight[u] + weight[v] - 2 * weight[query(u, v)];
  }
};

int main(){
  int n;
  cin >> n;
  LowestCommonAncestor lca(n);
  rep(i,n-1) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    lca.add_edge(u, v, w);
  }
  lca.build();
  int q;
  cin >> q;
  while (q--) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    ll d1 = lca.weightedsum(x, y);
    ll d2 = lca.weightedsum(y, z);
    ll d3 = lca.weightedsum(z, x);
    printf("%lld\n", (d1 + d2 + d3) / 2);
  }
  return 0;
}
0