結果

問題 No.898 tri-βutree
ユーザー niuezniuez
提出日時 2020-02-05 20:09:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 4,000 ms
コード長 3,210 bytes
コンパイル時間 2,034 ms
コンパイル使用メモリ 177,940 KB
実行使用メモリ 19,092 KB
最終ジャッジ日時 2023-08-08 16:36:48
合計ジャッジ時間 5,228 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
19,092 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 80 ms
12,784 KB
testcase_08 AC 80 ms
12,764 KB
testcase_09 AC 79 ms
12,784 KB
testcase_10 AC 77 ms
12,752 KB
testcase_11 AC 79 ms
12,856 KB
testcase_12 AC 78 ms
12,984 KB
testcase_13 AC 79 ms
12,796 KB
testcase_14 AC 80 ms
12,644 KB
testcase_15 AC 79 ms
12,644 KB
testcase_16 AC 79 ms
12,684 KB
testcase_17 AC 78 ms
12,644 KB
testcase_18 AC 81 ms
12,836 KB
testcase_19 AC 80 ms
12,756 KB
testcase_20 AC 78 ms
12,696 KB
testcase_21 AC 77 ms
12,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct HeavyLightDecomposition {
  vector<vector<pair<int, i64>>> G;
  vector<int> in;
  vector<int> sz;
  vector<int> next;
  vector<int> par;
  vector<i64> weight;


  HeavyLightDecomposition(i64 n)
    : G(n), in(n), sz(n), next(n, -1), par(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_sz(i64 v, i64 f, i64 W) {
    sz[v] = 1;
    weight[v] = W;
    for(i64 i = 0;i < G[v].size();i++) {
      
      i64 x = G[v][i].first;
      i64 w = G[v][i].second;
      if(x == f) continue;
      dfs_sz(x, v, W + w);
      par[x] = v;
      sz[v] += sz[x];
      if(sz[G[v][0].first] < sz[G[v][i].first]) { swap(G[v][0], G[v][i]); }
    }
  }

  i64 dfs_eul(i64 v, i64 f, i64 t) {
    in[v] = t++;
    for(i64 i = 0;i < G[v].size();i++) {
      i64 x = G[v][i].first;
      if(x == f) continue;
      next[x] = (i == 0) ? next[v] : x;
      t = dfs_eul(x, v, t);
    }
    return t;
  }

  void build(i64 r) {
    dfs_sz(r, -1, 0);
    dfs_eul(r, -1, 0);
  }

  i64 lca(i64 a, i64 b) const {
    while(true) {
      if(in[b] > in[a]) 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;

  HeavyLightDecomposition 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