結果

問題 No.898 tri-βutree
ユーザー Mayimg
提出日時 2019-10-06 13:28:07
言語 C++17(1z)
(gcc 8.3.0)
結果
AC  
実行時間 357 ms
コード長 2,787 Byte
コンパイル時間 2,809 ms
使用メモリ 26,644 KB
最終ジャッジ日時 2019-11-21 13:16:05

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
sample01 AC 9 ms
6,276 KB
test_1_01 AC 10 ms
6,308 KB
test_1_02 AC 9 ms
6,308 KB
test_1_03 AC 10 ms
6,312 KB
test_1_04 AC 8 ms
6,308 KB
test_1_05 AC 9 ms
6,312 KB
test_2_01 AC 302 ms
26,640 KB
test_2_02 AC 307 ms
26,640 KB
test_2_03 AC 320 ms
26,644 KB
test_2_04 AC 315 ms
26,640 KB
test_2_05 AC 357 ms
26,644 KB
test_2_06 AC 330 ms
26,376 KB
test_2_07 AC 317 ms
26,644 KB
test_2_08 AC 330 ms
26,640 KB
test_2_09 AC 343 ms
26,644 KB
test_2_10 AC 335 ms
26,640 KB
test_2_11 AC 307 ms
26,644 KB
test_2_12 AC 289 ms
26,640 KB
test_2_13 AC 305 ms
26,644 KB
test_2_14 AC 317 ms
26,640 KB
test_2_15 AC 344 ms
26,640 KB
テストケース一括ダウンロード

ソースコード

diff #
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
class LowestCommonAncestor {
private:
  static const int MAX_SIZE = 30;
  vector<vector<int>> graph;
  vector<int> parent[MAX_SIZE];
  vector<int> depth;
  int bit_length;
  void copy(const vector<int> g[], int siz) { 
    graph.resize(siz);
    for (int i = 0; i < siz; ++i) for (int j : g[i]) graph[i].push_back(j);
  }
  void copy(const vector<vector<int>>& g, int siz = -1) {
    if (siz < 0) siz = (int) g.size();
    graph.resize(siz);
    for (int i = 0; i < siz; ++i) for (int j : g[i]) graph[i].push_back(j);
  }
  void initialize() {
    int siz = (int) graph.size();
    int root = 0;
    bit_length = 1;
    while ((1 << bit_length) < siz) ++bit_length;
    for (int i = 0; i < bit_length; ++i) parent[i].resize(siz);
    depth.assign(siz, -1);
    dfs(root, -1, 0);
    for (int i = 0; i < bit_length- 1; ++i) {
      for (int v = 0; v < siz; ++v) {
        if (depth[v] == -1) continue;
        if (parent[i][v] < 0) parent[i + 1][v] = -1;
        else parent[i + 1][v] = parent[i][parent[i][v]];
      }
    }
  }
  void dfs(int cur, int par, int d) {
    parent[0][cur] = par;
    depth[cur] = d;
    for (int child : graph[cur]) if (child != par) dfs(child, cur, d + 1);
  }
public:
  LowestCommonAncestor() {}
  LowestCommonAncestor(const vector<vector<int>>& g, int siz = -1) { build(g, siz); }
  LowestCommonAncestor(const vector<int> g[], int siz) { build(g, siz); }
  void build(const vector<int> g[], int siz) { copy(g, siz); initialize(); }
  void build(const vector<vector<int>>& g, int siz = -1) { copy(g, siz); initialize(); }
  int get(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    for (int i = 0; i < bit_length; ++i) if (((depth[v] - depth[u]) >> i) & 1) v = parent[i][v];
    if (u == v) return u;
    for (int i = bit_length - 1; i >= 0; --i) if (parent[i][u] != parent[i][v]) u = parent[i][u], v = parent[i][v];
    return parent[0][u];
  }
};
using LCA = LowestCommonAncestor;
vector<pair<int, int>> g[101010];
vector<int> gg[101010];
long long dist[101010];
void dfs (int cur, int par, long long d) {
  dist[cur] = d;
  for (auto p : g[cur]) {
    if (p.first == par) continue;
    dfs(p.first, cur, d + p.second);
  }
}
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i + 1 < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
    gg[u].push_back(v);
    gg[v].push_back(u);
  }
  LCA lca(gg, n);
  dfs (0, -1, 0);
  int q;
  cin >> q;
  while (q--) {
    int x, y, z;
    cin >> x >> y >> z;
    cout << (dist[x] * 2 + dist[y] * 2 + dist[z] * 2 - dist[lca.get(x, y)] * 2 - dist[lca.get(y, z)] * 2 - dist[lca.get(z, x)] * 2) / 2 << '\n';
  } 
  return 0;
}
0