結果

問題 No.1094 木登り / Climbing tree
ユーザー MayimgMayimg
提出日時 2020-07-03 14:22:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 723 ms / 2,000 ms
コード長 4,722 bytes
コンパイル時間 3,411 ms
コンパイル使用メモリ 218,376 KB
実行使用メモリ 70,272 KB
最終ジャッジ日時 2024-11-08 06:18:50
合計ジャッジ時間 20,053 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 694 ms
59,008 KB
testcase_02 AC 203 ms
70,272 KB
testcase_03 AC 55 ms
5,888 KB
testcase_04 AC 181 ms
27,520 KB
testcase_05 AC 398 ms
51,584 KB
testcase_06 AC 206 ms
20,736 KB
testcase_07 AC 721 ms
59,008 KB
testcase_08 AC 723 ms
59,136 KB
testcase_09 AC 666 ms
59,008 KB
testcase_10 AC 688 ms
59,136 KB
testcase_11 AC 715 ms
59,136 KB
testcase_12 AC 699 ms
59,136 KB
testcase_13 AC 709 ms
59,136 KB
testcase_14 AC 686 ms
59,008 KB
testcase_15 AC 191 ms
17,920 KB
testcase_16 AC 483 ms
54,272 KB
testcase_17 AC 330 ms
33,408 KB
testcase_18 AC 290 ms
25,728 KB
testcase_19 AC 423 ms
45,952 KB
testcase_20 AC 709 ms
59,008 KB
testcase_21 AC 371 ms
35,840 KB
testcase_22 AC 680 ms
59,136 KB
testcase_23 AC 708 ms
59,136 KB
testcase_24 AC 667 ms
59,136 KB
testcase_25 AC 676 ms
59,136 KB
testcase_26 AC 699 ms
59,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;


/////////////////////////////////////////////////////////////////////////
// LCA
/////////////////////////////////////////////////////////////////////////
class LowestCommonAncestor {
public:
  LowestCommonAncestor();
  LowestCommonAncestor(const vector<vector<int>>& g, int r);
  LowestCommonAncestor(const vector<int> g[], int r, int siz);
  ~LowestCommonAncestor();
  void build(const vector<vector<int>>& g, int r);
  void build(const vector<int> g[], int r, int siz);
  inline int get(int u, int v) const;
  inline int getParent(int cur, int u) const;
  inline int getDepth(int cur) const;
  inline void debugTree () const;
private:
  void initialize();
  void dfs(int cur, int par, int d);
  static constexpr int MAX_SIZE = 30;
  vector<vector<int>> graph;
  vector<vector<int>> parent;
  vector<int> depth;
  int bit_;
  int root;
};

LowestCommonAncestor::LowestCommonAncestor() {};
LowestCommonAncestor::LowestCommonAncestor(const vector<vector<int>>& g, int r = -1) 
: graph(g), root(r) {
  if (root == -1) root = 0;
  initialize();
}
LowestCommonAncestor::LowestCommonAncestor(const vector<int> g[], int r = -1, int siz = -1) 
: root(r) {
  assert(siz > 0);
  if (root == -1) root = 0;
  graph.resize(siz);
  for (int i = 0; i < siz; i++) for (auto& j : g[i]) graph[i].push_back(j);
  initialize();
}
LowestCommonAncestor::~LowestCommonAncestor() {};
void LowestCommonAncestor::build(const vector<vector<int>>& g, int r = -1) {
  root = r;
  graph = g;
  if (root == -1) root = 0;
  initialize();
}
void LowestCommonAncestor::build(const vector<int> g[], int r = -1, int siz = -1) {
  assert(siz > 0);
  root = r;
  graph.resize(siz);
  for (int i = 0; i < siz; i++) for (auto& j : g[i]) graph[i].push_back(j);
  initialize();
}
inline int LowestCommonAncestor::get(int u, int v) const {
  assert(u >= 0 && u < (int) graph.size());
  assert(v >= 0 && v < (int) graph.size());
  if (depth[u] > depth[v]) swap(u, v);
  for (int i = 0; i < bit_; ++i) if (((depth[v] - depth[u]) >> i) & 1) v = parent[v][i];
  if (u == v) return u;
  for (int i = bit_ - 1; i >= 0; --i) if (parent[u][i] != parent[v][i]) u = parent[u][i], v = parent[v][i];
  return parent[u][0];
}
inline int LowestCommonAncestor::getParent(int cur, int u = 0) const {
  if ((int) parent[cur].size() <= u) return -1;
  //assert(cur >= 0 && cur < (int) parent.size());
  //assert(u >= 0 && (int) parent[cur].size() > u);
  return parent[cur][u];
}
inline int LowestCommonAncestor::getDepth(int cur) const {
  assert(cur >= 0 && cur < (int) depth.size());
  return depth[cur];
}
void LowestCommonAncestor::initialize() {
  bit_ = 1;
  while ((1 << bit_) < (int) graph.size()) ++bit_;
  parent = vector<vector<int>>((int) graph.size(), vector<int>(bit_));
  depth.assign((int) graph.size(), -1);
  dfs(root, -1, 0);
  for (int i = 0; i < bit_ - 1; ++i) {
    for (int v = 0; v < (int) graph.size(); ++v) {
      if (depth[v] == -1) continue;
      if (parent[v][i] < 0) parent[v][i + 1] = -1;
      else parent[v][i + 1] = parent[parent[v][i]][i];
    }
  }
}
void LowestCommonAncestor::dfs(int cur, int par, int d) {
  parent[cur][0] = par;
  depth[cur] = d;
  for (auto& child : graph[cur]) if (child != par) dfs(child, cur, d + 1);
}
void LowestCommonAncestor::debugTree () const {
  cerr << "size " << parent.size() << endl;
  for (int i = 0; i < (int) parent.size(); i++) {
    cerr << "the parent of " << i << " -> " << parent[i][0] << endl;
  }
  cerr << endl;
  for (int i = 0; i < (int) graph.size(); i++) {
    cerr << "the neighbor of " << i << " -> ";
    for (const int& j : graph[i]) cerr << j << " ";
    cerr << endl;
  }
}
using LCA = LowestCommonAncestor;
/////////////////////////////////////////////////////////////////////////
// END LCA
/////////////////////////////////////////////////////////////////////////

vector<int> dist;
vector<vector<pair<int, int>>> g;
vector<vector<int>> gg;
void dfs (int cur, int par) {
  for (auto child : g[cur]) if (child.first != par) {
    dist[child.first] = dist[cur] + child.second;
    dfs(child.first, cur);
  }
}

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  g = vector<vector<pair<int, int>>>(n);
  gg = vector<vector<int>>(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    int c;
    cin >> c;
    g[u].emplace_back(v, c);
    g[v].emplace_back(u, c);
    gg[u].push_back(v);
    gg[v].push_back(u);
  }
  dist = vector<int>(n);
  dfs(0, -1);
  LCA lca(gg);
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    cout << dist[u] - dist[lca.get(u, v)] * 2 + dist[v] << '\n';
  }
  return 0;
}
0