結果

問題 No.898 tri-βutree
ユーザー niuezniuez
提出日時 2020-04-26 11:16:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 4,000 ms
コード長 3,718 bytes
コンパイル時間 904 ms
コンパイル使用メモリ 80,652 KB
実行使用メモリ 14,556 KB
最終ジャッジ日時 2023-08-08 16:45:28
合計ジャッジ時間 4,597 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
14,012 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,388 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 81 ms
14,248 KB
testcase_08 AC 83 ms
14,288 KB
testcase_09 AC 79 ms
14,460 KB
testcase_10 AC 83 ms
14,500 KB
testcase_11 AC 81 ms
14,488 KB
testcase_12 AC 82 ms
14,392 KB
testcase_13 AC 83 ms
14,288 KB
testcase_14 AC 82 ms
14,428 KB
testcase_15 AC 82 ms
14,404 KB
testcase_16 AC 80 ms
14,396 KB
testcase_17 AC 81 ms
14,556 KB
testcase_18 AC 88 ms
14,296 KB
testcase_19 AC 87 ms
14,344 KB
testcase_20 AC 86 ms
14,340 KB
testcase_21 AC 85 ms
14,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
using i64 = long long;

struct heavy_light_decomposition {
  int N;
  std::vector<std::vector<std::pair<int, i64>>> G;
  std::vector<int> in;
  std::vector<int> out;
  std::vector<int> sz;
  std::vector<int> next;
  std::vector<int> par;

  std::vector<i64> weight;

  heavy_light_decomposition(int n)
    : N(n), G(n), in(n), out(n, -1), sz(n, 1), next(n, -1), par(n, -1), weight(n) {}

  void add_edge(int u, int v, i64 w) {
    G[u].push_back({ v, w });
    G[v].push_back({ u, w });
  }

  void build(int r) {
    std::vector<int> st(N);
    std::vector<int> seq(N);
    std::vector<int> idx(N, -1);
    int si = 0;
    int qi = 0;
    st[si++] = r;
    while(si > 0) {
      int v = st[--si];
      st[si] = 0;
      seq[qi++] = v;
      for(int i = 0; i < G[v].size(); i++) {
        int t = G[v][i].first;
        if(par[v] == t) continue;
        par[t] = v;
        idx[t] = i;
        st[si++] = t;

        weight[t] = weight[v] + G[v][i].second;
      }
    }

    for(int i = N; i --> 0;) {
      int v = seq[i];
      int p = par[v];
      out[v] = std::max(out[v], i + 1);
      if(p >= 0) {
        sz[p] += sz[v];
        out[p] = std::max(out[p], out[v]);
        if(st[p] < sz[v]) {
          st[p] = sz[v];
          std::swap(G[p][0], G[p][idx[v]]);
        }
      }
    }

    si = 0;
    qi = 0;

    st[si++] = r;
    while(si > 0) {
      int v = st[--si];
      in[v] = qi++;
      for(int i = G[v].size(); i --> 0;) {
        int t = G[v][i].first;
        if(par[v] == t) continue;
        next[t] = (i == 0) ? next[v] : t;
        st[si++] = t;
      }
    }
  }

  i64 lca(i64 a, i64 b) const {
    while(true) {
      if(in[b] > in[a]) std::swap(a, b);
      if(next[b] == next[a]) return b;
      a = par[next[a]];
    }
  }
};

#include <cstdio>

namespace niu {
  char cur;
  struct FIN {
    static inline bool is_blank(char c) { return c <= ' '; }
    inline char next() { return cur = getc_unlocked(stdin); }
    inline char peek() { return cur; }
    inline void skip() { while(is_blank(next())){} }
#define intin(inttype)  \
    FIN& operator>>(inttype& n) { \
      bool sign = 0; \
      n = 0; \
      skip(); \
      while(!is_blank(peek())) { \
        if(peek() == '-') sign = 1; \
        else n = (n << 1) + (n << 3) + (peek() & 0b1111); \
        next(); \
      } \
      if(sign) n = -n; \
      return *this; \
    }
    intin(int)
      intin(long long)
  } fin;

  char tmp[128];
  struct FOUT {
    static inline bool is_blank(char c) { return c <= ' '; }
    inline void push(char c) { putc_unlocked(c, stdout); }
    FOUT& operator<<(char c) { push(c); return *this; }
    FOUT& operator<<(const char* s) { while(*s) push(*s++); return *this; }
#define intout(inttype) \
    FOUT& operator<<(inttype n) { \
      if(n) { \
        char* p = tmp + 127; bool neg = 0; \
        if(n < 0) neg = 1, n = -n; \
        while(n) *--p = (n % 10) | 0b00110000, n /= 10; \
        if(neg) *--p = '-'; \
        return (*this) << p; \
      } \
      else { \
        push('0'); \
        return *this; \
      } \
    }
    intout(int)
      intout(long long)
  } fout;
}
#include <iostream>

int main() {
  using niu::fin;
  using niu::fout;
  using i64 = long long;
  i64 N;
  fin >> N;

  heavy_light_decomposition eul(N);
  for(int i = 0;i < N - 1;i++) {
    i64 a, b, c;
    niu::fin >> a >> b >> c;
    eul.add_edge(a, b, c);
  }

  eul.build(0);

  auto dist = [&](int a, int b) -> i64 {
    int c = eul.lca(a, b);
    return eul.weight[a] + eul.weight[b] - 2 * eul.weight[c];
  };

  i64 Q;
  fin >> Q;
  for(i64 q = 0; q < Q; q++) {
    i64 x, y, z;
    fin >> x >> y >> z;
    fout << (dist(x, y) + dist(y, z) + dist(z, x)) / 2 << "\n";
  }
}
0