結果

問題 No.898 tri-βutree
ユーザー niuezniuez
提出日時 2019-09-16 21:32:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 571 ms / 4,000 ms
コード長 2,431 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 180,856 KB
実行使用メモリ 95,196 KB
最終ジャッジ日時 2024-04-26 08:18:27
合計ジャッジ時間 13,197 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 416 ms
95,196 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 571 ms
90,400 KB
testcase_08 AC 539 ms
90,560 KB
testcase_09 AC 541 ms
90,436 KB
testcase_10 AC 541 ms
90,556 KB
testcase_11 AC 555 ms
90,428 KB
testcase_12 AC 539 ms
90,424 KB
testcase_13 AC 544 ms
90,428 KB
testcase_14 AC 543 ms
90,556 KB
testcase_15 AC 537 ms
90,436 KB
testcase_16 AC 539 ms
90,556 KB
testcase_17 AC 536 ms
90,432 KB
testcase_18 AC 539 ms
90,332 KB
testcase_19 AC 540 ms
90,288 KB
testcase_20 AC 550 ms
90,560 KB
testcase_21 AC 541 ms
90,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define let auto const

struct eulartour_subtree {
  vector<vector<pair<i64, i64>>> G;
  vector<i64> tour;
  vector<i64> L, R;
  vector<i64> depth;
  vector<i64> weight;
  eulartour_subtree(i64 n): G(n), L(n), R(n), depth(n), weight(n) {}
  void add_edge(i64 u, i64 v, i64 w) {
    G[u].push_back({v, w});
    G[v].push_back({u, w});
  }

  void dfs(i64 v, i64 f, i64 d, i64 w) {
    tour.push_back(v);
    L[v] = tour.size() - 1;
    depth[v] = d;
    weight[v] = w;
    for(auto to: G[v]) {
      if(to.first == f) continue;
      dfs(to.first, v, d + 1, w + to.second);
      tour.push_back(v);
    }
    R[v] = tour.size() - 1;
  }

  i64 start_tour(i64 r) {
    dfs(r, -1, 0, 0);
    return tour.size();
  }
};

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

struct sparse_table {
  using Band = pair<i64, i64>;
  Band ope(const Band& a, const Band b) { return min(a, b); }

  i64 N;
  vector<Band> A;
  vector<i64> log_t;
  vector<vector<Band>> table;

  sparse_table(vector<Band> arr) : N(arr.size()), A(arr), log_t(arr.size() + 1) {
    for(int i = 2;i < N + 1;i++) {
      log_t[i] = log_t[i >> 1] + 1;
    }
    table.resize(N, vector<Band>(log_t[N] + 1));

    for(int i = 0;i < N;i++) {
      table[i][0] = arr[i];
    }

    for(int k = 1;(1 << k) <= N;k++) {
      for(int i = 0;i + (1 << k) <= N;i++) {
        table[i][k] = ope(table[i][k - 1], table[i + (1 << (k - 1))][k - 1]);
      }
    }
  }
  /* [s, t] */
  Band query(i64 s, i64 t) {
    i64 k = log_t[t - s + 1];
    return ope(table[s][k], table[t - (1 << k) + 1][k]);
  }
};

int main() {
  i64 N;
  cin >> N;

  eulartour_subtree eul(N);
  for(int i = 0;i < N - 1;i++) {
    i64 a, b, c;
    cin >> a >> b >> c;
    eul.add_edge(a, b, c);
  }
  i64 sz = eul.start_tour(0);

  vector<pair<i64, i64>> arr(sz);
  for(i64 i = 0;i < sz;i++) {
    arr[i] = { eul.depth[eul.tour[i]], eul.tour[i] };
  }
  sparse_table spa(arr);

  auto dist = [&](i64 a, i64 b) -> i64 {
    i64 c = spa.query(min(eul.L[a], eul.L[b]), max(eul.R[a], eul.R[b])).second;
    return eul.weight[a] + eul.weight[b] - 2 * eul.weight[c];
  };

  i64 Q;
  cin >> Q;
  for(i64 q = 0; q < Q; q++) {
    i64 x, y, z;
    cin >> x >> y >> z;
    cout << (dist(x, y) + dist(y, z) + dist(x, z)) / 2 << endl;
  }
}
0