結果

問題 No.1094 木登り / Climbing tree
ユーザー 👑 emthrmemthrm
提出日時 2020-06-24 02:04:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 3,035 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 214,952 KB
実行使用メモリ 55,556 KB
最終ジャッジ日時 2024-04-25 18:23:00
合計ジャッジ時間 13,463 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 352 ms
49,196 KB
testcase_02 AC 112 ms
55,556 KB
testcase_03 AC 42 ms
6,944 KB
testcase_04 AC 90 ms
22,784 KB
testcase_05 AC 168 ms
43,112 KB
testcase_06 AC 134 ms
17,152 KB
testcase_07 AC 383 ms
49,400 KB
testcase_08 AC 373 ms
49,352 KB
testcase_09 AC 379 ms
49,324 KB
testcase_10 AC 368 ms
49,428 KB
testcase_11 AC 385 ms
49,292 KB
testcase_12 AC 370 ms
49,460 KB
testcase_13 AC 386 ms
49,332 KB
testcase_14 AC 388 ms
49,296 KB
testcase_15 AC 132 ms
14,336 KB
testcase_16 AC 274 ms
42,912 KB
testcase_17 AC 196 ms
26,528 KB
testcase_18 AC 166 ms
20,604 KB
testcase_19 AC 229 ms
36,032 KB
testcase_20 AC 392 ms
49,480 KB
testcase_21 AC 192 ms
28,100 KB
testcase_22 AC 393 ms
49,440 KB
testcase_23 AC 384 ms
49,316 KB
testcase_24 AC 368 ms
49,228 KB
testcase_25 AC 378 ms
49,404 KB
testcase_26 AC 386 ms
49,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = ll;
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) REP(ver, n) {
      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);
    REP(i, table_h) {
      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);
  REP(_, n - 1) {
    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;
}
0