結果

問題 No.1094 木登り / Climbing tree
ユーザー kenjihkenjih
提出日時 2021-05-21 19:34:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 609 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 208,860 KB
実行使用メモリ 41,444 KB
最終ジャッジ日時 2024-04-25 19:40:29
合計ジャッジ時間 17,558 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 548 ms
34,528 KB
testcase_02 AC 96 ms
41,444 KB
testcase_03 AC 159 ms
6,944 KB
testcase_04 AC 122 ms
16,448 KB
testcase_05 AC 170 ms
30,396 KB
testcase_06 AC 330 ms
12,436 KB
testcase_07 AC 554 ms
34,516 KB
testcase_08 AC 556 ms
34,528 KB
testcase_09 AC 544 ms
34,400 KB
testcase_10 AC 576 ms
34,568 KB
testcase_11 AC 601 ms
34,528 KB
testcase_12 AC 576 ms
34,532 KB
testcase_13 AC 562 ms
34,528 KB
testcase_14 AC 544 ms
34,568 KB
testcase_15 AC 324 ms
11,220 KB
testcase_16 AC 447 ms
32,388 KB
testcase_17 AC 389 ms
20,268 KB
testcase_18 AC 349 ms
15,900 KB
testcase_19 AC 401 ms
27,048 KB
testcase_20 AC 570 ms
34,528 KB
testcase_21 AC 402 ms
21,372 KB
testcase_22 AC 584 ms
34,528 KB
testcase_23 AC 609 ms
34,528 KB
testcase_24 AC 556 ms
34,528 KB
testcase_25 AC 581 ms
34,652 KB
testcase_26 AC 528 ms
34,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template<typename T = long long>
class lca {
  int n;
  vector<vector<pair<int, T> > > edges;
  vector<int> depth;
  vector<vector<int> > par;
  vector<T> dist;
  
  void dfs(int v, int p = -1, int d = 0, T l = 0) {
    par[0][v] = p;
    depth[v] = d;
    dist[v] = l;
    for (auto &[u, w]: edges[v]) {
      if (u == p) continue;
      dfs(u, v, d + 1, l + w);
    }
  }
  
public:
  lca(int n): n(n) {
    edges = vector<vector<pair<int, T> > >(n);
    depth = vector<int>(n);
    int m = 1;
    while ((1<<m) < n) ++m;
    par = vector<vector<int> >(m, vector<int>(n));
    dist = vector<T>(n);
  }
  
  void add_edge(int v, int w, T l = 0) {
    edges[v].emplace_back(w, l);
    edges[w].emplace_back(v, l);
  }
  
  void build() {    
    dfs(0);
    for (int i = 1; i < par.size(); i++) {
      for (int j = 0; j < n; j++) {
        par[i][j] = par[i-1][j] == -1 ? -1 : par[i-1][par[i-1][j]];
      }
    }
  }
  
  int get(int v, int w) {
    if (depth[w] > depth[v])
      swap(v, w);
    int d = depth[v] - depth[w];
    for (int i = 0; i < par.size(); i++) {
      if (d & 1 << i) {
        v = par[i][v];
      }
    }
    
    if (v == w) return v;
    for (int i = par.size() - 1; i >= 0; i--) {
      if (par[i][v] != par[i][w]) {
        v = par[i][v];
        w = par[i][w];
      }
    }
    return par[0][v];
  }
  
  int get_length(int u, int v) {
    int w = get(u, v);
    return depth[u] + depth[v] - 2 * depth[w]; 
  }
  
  T get_dist(int u, int v) {
    int w = get(u, v);
    return dist[u] + dist[v] - 2 * dist[w];
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  lca tree(n);
  for (int i = 0; i < n-1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    tree.add_edge(a, b, c);
  }

  tree.build();
  
  int q;
  cin >> q;
  while (q--) {
    int s, t;
    cin >> s >> t;
    --s, --t;
    cout << tree.get_dist(s, t) << endl;
  }
  
  return 0;
}
0