結果
| 問題 | 
                            No.1094 木登り / Climbing tree
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-06-27 14:09:30 | 
| 言語 | C++17(clang)  (17.0.6 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                RE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,314 bytes | 
| コンパイル時間 | 2,552 ms | 
| コンパイル使用メモリ | 166,912 KB | 
| 実行使用メモリ | 824,984 KB | 
| 最終ジャッジ日時 | 2024-11-30 18:55:05 | 
| 合計ジャッジ時間 | 64,714 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | RE * 1 | 
| other | RE * 8 TLE * 17 MLE * 1 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using CostType = long long;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};
struct LCADoubling {
  vector<int> depth;
  vector<CostType> dist;
  LCADoubling(const vector<vector<Edge>> &graph) : graph(graph) {
    n = graph.size();
    depth.resize(n);
    dist.resize(n);
    while ((1 << table_h) <= n) ++table_h;
    parent.resize(table_h, vector<int>(n));
  }
  void build(int root = 0) {
    dfs(-1, root, 0, 0);
    for (int i = 0; i + 1 < table_h; ++i) for(int ver=0; ver<n; ver++) {
      parent[i + 1][ver] = (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]);
    }
  }
  int query(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    for(int i=0;i<table_h;i++) {
      if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
    }
    if (u == v) return u;
    for (int i = table_h - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }
  CostType distance(int u, int v) { return dist[u] + dist[v] - dist[query(u, v)] * 2; }
private:
  int n, table_h = 1;
  vector<vector<Edge>> graph;
  vector<vector<int>> parent;
  void dfs(int par, int ver, int now_depth, CostType now_dist) {
    depth[ver] = now_depth;
    dist[ver] = now_dist;
    parent[0][ver] = par;
    for (const Edge &e : graph[ver]) {
      if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost);
    }
  }
};
int main() {
    int n; cin >> n;
    vector<vector<Edge>> graph(n);
    for(int i=0;i<n;i++) {
        int a,b,c; cin >> a >> b >> c; a--; b--;
        graph[a].emplace_back(a, b, c);
        graph[b].emplace_back(b, a, c);
    }
    LCADoubling lca(graph);
    lca.build();
    int q; cin >> q;
    while(q--) {
        int s, t; cin >> s >> t; --s; --t;
        cout << lca.distance(s, t) << '\n';
    }
    return 0;
}