結果

問題 No.1094 木登り / Climbing tree
ユーザー kk
提出日時 2020-09-02 01:54:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 1,763 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 211,444 KB
実行使用メモリ 53,844 KB
最終ジャッジ日時 2023-08-08 01:27:44
合計ジャッジ時間 21,160 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 707 ms
33,296 KB
testcase_02 AC 112 ms
53,844 KB
testcase_03 AC 179 ms
4,612 KB
testcase_04 AC 149 ms
15,900 KB
testcase_05 AC 211 ms
29,076 KB
testcase_06 AC 377 ms
12,608 KB
testcase_07 AC 720 ms
32,980 KB
testcase_08 AC 710 ms
32,920 KB
testcase_09 AC 706 ms
32,988 KB
testcase_10 AC 730 ms
33,096 KB
testcase_11 AC 717 ms
33,032 KB
testcase_12 AC 719 ms
33,032 KB
testcase_13 AC 730 ms
33,072 KB
testcase_14 AC 729 ms
32,964 KB
testcase_15 AC 428 ms
14,160 KB
testcase_16 AC 609 ms
41,740 KB
testcase_17 AC 532 ms
26,040 KB
testcase_18 AC 495 ms
20,116 KB
testcase_19 AC 576 ms
35,412 KB
testcase_20 AC 725 ms
32,984 KB
testcase_21 AC 536 ms
27,516 KB
testcase_22 AC 711 ms
32,988 KB
testcase_23 AC 713 ms
32,992 KB
testcase_24 AC 714 ms
33,060 KB
testcase_25 AC 713 ms
32,960 KB
testcase_26 AC 712 ms
32,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

void dfs(int v, int p, int d, long long s,
         vector<int> &par, vector<int> &depth, vector<long long> &sum,
         vector<vector<pair<int, int> > > &edges) {

  par[v] = p;
  depth[v] = d;
  sum[v] = s;

  for (auto &pr: edges[v]) {
    int w, cost;
    tie(w, cost) = pr;
    if (w == p) continue;
    dfs(w, v, d+1, s + cost, par, depth, sum, edges); 
  }
}

int lca(int v, int w, const vector<vector<int> > &par, const vector<int> &depth) {
  if (depth[w] > depth[v])
    swap(v, w);

  for (int i = 0; i < par.size(); i++) {
    int d = depth[v] - depth[w];
    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 main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  vector<vector<pair<int, int> > > edges(n);
  REP (i, n-1) {
    int v, w, c;
    cin >> v >> w >> c;
    --v, --w;
    edges[v].emplace_back(w, c);
    edges[w].emplace_back(v, c);
  }

  vector<int> par(n);
  vector<int> depth(n);
  vector<long long> sum(n);

  dfs(0, -1, 0, 0, par, depth, sum, edges);

  vector<vector<int> > dp(18, vector<int>(n));
  REP (i, n) dp[0][i] = par[i];
  for (int i = 1; i < 18; i++) {
    REP (j, n) {
      int tmp = dp[i-1][j];
      if (tmp == -1)
        dp[i][j] = -1;
      else
        dp[i][j] = dp[i-1][tmp];
    }
  }
  
  int Q;
  cin >> Q;

  while (Q--) {
    int s, t;
    cin >> s >> t;
    --s, --t;
    int w = lca(s, t, dp, depth);
    long long ret = sum[s] + sum[t] - 2 * sum[w];
    cout << ret << endl;
  }
  
  return 0;
}
0