結果

問題 No.898 tri-βutree
ユーザー ninja-kidninja-kid
提出日時 2023-02-14 11:32:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 476 ms / 4,000 ms
コード長 2,154 bytes
コンパイル時間 4,973 ms
コンパイル使用メモリ 266,508 KB
実行使用メモリ 23,812 KB
最終ジャッジ日時 2023-09-23 21:46:19
合計ジャッジ時間 17,553 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 276 ms
23,812 KB
testcase_01 AC 3 ms
9,612 KB
testcase_02 AC 3 ms
9,592 KB
testcase_03 AC 3 ms
9,620 KB
testcase_04 AC 3 ms
9,620 KB
testcase_05 AC 3 ms
9,612 KB
testcase_06 AC 3 ms
9,744 KB
testcase_07 AC 446 ms
19,528 KB
testcase_08 AC 455 ms
19,484 KB
testcase_09 AC 476 ms
19,532 KB
testcase_10 AC 471 ms
19,488 KB
testcase_11 AC 470 ms
19,652 KB
testcase_12 AC 464 ms
19,644 KB
testcase_13 AC 448 ms
19,580 KB
testcase_14 AC 450 ms
19,584 KB
testcase_15 AC 463 ms
19,648 KB
testcase_16 AC 449 ms
19,456 KB
testcase_17 AC 455 ms
19,596 KB
testcase_18 AC 457 ms
19,444 KB
testcase_19 AC 457 ms
19,580 KB
testcase_20 AC 451 ms
19,508 KB
testcase_21 AC 455 ms
19,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace atcoder;
using ll = long long;
const ll MOD1 = 1000000007LL;
const ll MOD2 = 998244353LL;
using namespace std;
const vector<pair<int, int>> dpos4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
// const vector<pair<int, int>> dpos8 = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
int N;
vector<vector<pair<int, ll>>> edges;
int depth[100100];
ll dist[100100];
int parent[20][100100];

void dfs(int fr, int prev = -1){
  for(auto [to, cost]: edges[fr]){
    if(to == prev) continue;
    depth[to] = depth[fr] + 1;
    dist[to] = dist[fr] + cost;
    parent[0][to] = fr;
    dfs(to, fr);
  }
}

int la(int fr, int h){
  for(int i = 19; i >= 0; i--){
    if((1<<i) <= h){
      fr = parent[i][fr];
      h -= (1 << i);
    }
  }
  return fr;
}

int lca(int a, int b){
  if(depth[a] < depth[b]) swap(a, b);
  a = la(a, depth[a] - depth[b]);
  if(a == b) return a;
  for(int i = 19; i >= 0; i--){
    if(parent[i][a] == parent[i][b]) continue;
    a = parent[i][a];
    b = parent[i][b];
  }
  return parent[0][a];
}

ll func(int a, int b){
  int p = lca(a, b);
  return dist[a] + dist[b] - 2 * dist[p];
}

int main() {
  cin >> N;
  edges.resize(N);
  rep(i, N - 1){
    int a, b;
    ll c;
    cin >> a >> b >> c;
    edges[a].emplace_back(b, c);
    edges[b].emplace_back(a, c);
  }
  
  depth[0] = 0;
  dist[0] = 0;
  parent[0][0] = 0;
  dfs(0);
  
  for(int i = 1; i < 20; i++){
    rep(j, N){
      parent[i][j] = parent[i - 1][parent[i - 1][j]];
    }
  }
  
  int Q;
  cin >> Q;
  rep(i, Q){
    int a, b, c;
    cin >> a >> b >> c;
    ll ans = func(a, b) + func(b, c) + func(c, a);
    ans /= 2;
    cout << ans << '\n';
  }
  
  return 0;
}
0