結果

問題 No.1094 木登り / Climbing tree
ユーザー iqueue02iqueue02
提出日時 2020-07-03 10:53:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 372 ms / 2,000 ms
コード長 2,110 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 212,168 KB
実行使用メモリ 37,192 KB
最終ジャッジ日時 2023-08-08 00:39:33
合計ジャッジ時間 13,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 347 ms
30,592 KB
testcase_02 AC 94 ms
37,192 KB
testcase_03 AC 44 ms
4,480 KB
testcase_04 AC 85 ms
14,884 KB
testcase_05 AC 167 ms
26,848 KB
testcase_06 AC 129 ms
11,092 KB
testcase_07 AC 372 ms
30,600 KB
testcase_08 AC 367 ms
30,556 KB
testcase_09 AC 356 ms
30,624 KB
testcase_10 AC 368 ms
30,600 KB
testcase_11 AC 369 ms
30,712 KB
testcase_12 AC 359 ms
30,736 KB
testcase_13 AC 356 ms
30,600 KB
testcase_14 AC 354 ms
30,656 KB
testcase_15 AC 130 ms
10,312 KB
testcase_16 AC 301 ms
29,200 KB
testcase_17 AC 210 ms
18,052 KB
testcase_18 AC 166 ms
14,184 KB
testcase_19 AC 252 ms
24,300 KB
testcase_20 AC 359 ms
30,604 KB
testcase_21 AC 192 ms
19,200 KB
testcase_22 AC 351 ms
30,524 KB
testcase_23 AC 341 ms
30,736 KB
testcase_24 AC 345 ms
30,604 KB
testcase_25 AC 340 ms
30,596 KB
testcase_26 AC 358 ms
30,736 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 long double ld;
typedef pair<int,int> P;
typedef pair<ll, int> PL;
constexpr int INF = 2e9;

struct LowestCommonAncestor {
  int n, h;
  vector<vector<int>> par;
  vector<vector<pair<int,int>>> g;
  vector<int> dep;
  vector<int> 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)];
  }

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr); 
  int n;
  cin >> n;
  LowestCommonAncestor lca(n);

  rep(i,n-1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    lca.add_edge(a, b, c);
  }

  lca.build();

  int q;
  cin >> q;
  
  while (q--) {

    int s, t;
    cin >> s >> t;
    s--; t--;

    cout << lca.weight_sum(s, t) << '\n';

  }
  return 0;
} 
0