結果

問題 No.1094 木登り / Climbing tree
ユーザー SSRSSSRS
提出日時 2021-12-19 17:15:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,585 bytes
コンパイル時間 1,874 ms
コンパイル使用メモリ 180,552 KB
実行使用メモリ 41,396 KB
最終ジャッジ日時 2023-10-13 18:53:29
合計ジャッジ時間 24,852 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 662 ms
39,872 KB
testcase_02 AC 186 ms
41,396 KB
testcase_03 AC 179 ms
4,892 KB
testcase_04 AC 166 ms
19,088 KB
testcase_05 AC 236 ms
35,196 KB
testcase_06 AC 375 ms
14,680 KB
testcase_07 AC 706 ms
39,872 KB
testcase_08 AC 696 ms
39,800 KB
testcase_09 AC 686 ms
39,888 KB
testcase_10 AC 691 ms
39,764 KB
testcase_11 AC 677 ms
39,888 KB
testcase_12 AC 691 ms
39,808 KB
testcase_13 AC 697 ms
39,968 KB
testcase_14 AC 705 ms
39,912 KB
testcase_15 AC 412 ms
11,432 KB
testcase_16 WA -
testcase_17 AC 467 ms
20,476 KB
testcase_18 AC 439 ms
15,988 KB
testcase_19 AC 530 ms
27,792 KB
testcase_20 AC 695 ms
39,884 KB
testcase_21 AC 476 ms
21,540 KB
testcase_22 AC 696 ms
39,884 KB
testcase_23 AC 674 ms
39,752 KB
testcase_24 AC 682 ms
39,928 KB
testcase_25 AC 697 ms
39,876 KB
testcase_26 AC 686 ms
39,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int LOG = 17;
int main(){
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++){
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    E[a].push_back(make_pair(c, b));
    E[b].push_back(make_pair(c, a));
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<int> d1(N, 0);
  vector<int> d2(N, 0);
  queue<int> q;
  q.push(0);
  while (!q.empty()){
    int v = q.front();
    q.pop();
    for (auto P : E[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        d1[w] = d1[v] + 1;
        d2[w] = d2[v] + P.first;
        q.push(w);
      }
    }
  }
  vector<vector<int>> pp(LOG, vector<int>(N, -1));
  pp[0] = p;
  for (int i = 0; i < LOG - 1; i++){
    for (int j = 0; j < N; j++){
      if (pp[i][j] != -1){
        pp[i + 1][j] = pp[i][pp[i][j]];
      }
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    int ans = 0;
    if (d1[s] > d1[t]){
      swap(s, t);
    }
    for (int j = 0; j < LOG; j++){
      if (((d1[t] - d1[s]) >> j & 1) == 1){
        ans += d2[t] - d2[pp[j][t]];
        t = pp[j][t];
      }
    }
    if (s != t){
      for (int j = LOG - 1; j >= 0; j--){
        if (pp[j][s] != pp[j][t]){
          ans += d2[s] - d2[pp[j][s]];
          ans += d2[t] - d2[pp[j][t]];
          s = pp[j][s];
          t = pp[j][t];
        }
      }
      ans += d2[s] - d2[p[s]];
      ans += d2[t] - d2[p[t]];
    }
    cout << ans << endl;
  }
}
0