結果

問題 No.1094 木登り / Climbing tree
ユーザー kk
提出日時 2020-09-02 02:08:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 580 ms / 2,000 ms
コード長 1,968 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 206,804 KB
実行使用メモリ 53,260 KB
最終ジャッジ日時 2024-04-25 19:27:57
合計ジャッジ時間 17,924 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 580 ms
32,300 KB
testcase_02 AC 108 ms
53,260 KB
testcase_03 AC 160 ms
6,940 KB
testcase_04 AC 119 ms
15,488 KB
testcase_05 AC 164 ms
28,612 KB
testcase_06 AC 312 ms
11,776 KB
testcase_07 AC 567 ms
32,420 KB
testcase_08 AC 546 ms
32,296 KB
testcase_09 AC 553 ms
32,292 KB
testcase_10 AC 572 ms
32,428 KB
testcase_11 AC 570 ms
32,436 KB
testcase_12 AC 566 ms
32,448 KB
testcase_13 AC 566 ms
32,580 KB
testcase_14 AC 552 ms
32,400 KB
testcase_15 AC 354 ms
13,696 KB
testcase_16 AC 478 ms
41,260 KB
testcase_17 AC 422 ms
25,472 KB
testcase_18 AC 391 ms
19,712 KB
testcase_19 AC 490 ms
34,688 KB
testcase_20 AC 575 ms
32,348 KB
testcase_21 AC 438 ms
27,008 KB
testcase_22 AC 569 ms
32,416 KB
testcase_23 AC 553 ms
32,360 KB
testcase_24 AC 569 ms
32,456 KB
testcase_25 AC 551 ms
32,408 KB
testcase_26 AC 571 ms
32,292 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); 
  }
}

vector<vector<int> > doubling(const vector<int> &v) {
  int n = v.size();
  int m = 1;
  while ((1LL<<m+1) < n) ++m;
  vector<vector<int> > dp(m, vector<int>(n));
  for (int i = 0; i < n; i++) dp[0][i] = v[i];
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < n; j++) {
      int tmp = dp[i-1][j];
      if (tmp == -1)
        dp[i][j] = -1;
      else
        dp[i][j] = dp[i-1][tmp];
    }
  }
  return dp;
}

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 = doubling(par);
  
  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