結果

問題 No.898 tri-βutree
ユーザー pentapenta
提出日時 2020-06-07 17:58:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 4,000 ms
コード長 3,256 bytes
コンパイル時間 2,278 ms
コンパイル使用メモリ 185,556 KB
実行使用メモリ 36,544 KB
最終ジャッジ日時 2023-08-08 16:49:58
合計ジャッジ時間 7,890 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
36,544 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 195 ms
32,288 KB
testcase_08 AC 200 ms
32,324 KB
testcase_09 AC 193 ms
32,300 KB
testcase_10 AC 197 ms
32,364 KB
testcase_11 AC 202 ms
32,288 KB
testcase_12 AC 196 ms
32,316 KB
testcase_13 AC 200 ms
32,296 KB
testcase_14 AC 198 ms
32,304 KB
testcase_15 AC 194 ms
32,324 KB
testcase_16 AC 196 ms
32,436 KB
testcase_17 AC 208 ms
32,300 KB
testcase_18 AC 208 ms
32,296 KB
testcase_19 AC 194 ms
32,568 KB
testcase_20 AC 195 ms
32,304 KB
testcase_21 AC 209 ms
32,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) a = b; }
template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) a = b; }

template<typename T>
struct LowestCommonAncestor {
  private:
    vector<vector<int> > par;
    vector<int> dist; //from root
    vector<T> weight; //from root
    vector<vector<T> > max_w;
    const vector<vector<pair<int,T> > > &G;

    void dfs_(int v, int p, int d, T w) {
      par[0][v] = p;
      dist[v] = d;
      weight[v] = w;
      for (auto to : G[v]) {
        if (to.first == p) continue; 
        dfs_(to.first, v, d+1, w+to.second);
        max_w[0][to.first] = to.second;
      }
    }
    int query(int u,int v) {
      assert(u < (int)dist.size());
      assert(v < (int)dist.size());
      if (dist[u] > dist[v]) swap(u,v);
      int k = par.size();
      for (int i = 0; i < k; ++i) { //LCAまでの距離を揃える
        if (((dist[v]-dist[u])>>i) & 1) v = par[i][v];
      }
      if (u == v) return u;
      for (int i = k-1; i >= 0; --i) {
        if (par[i][u] == par[i][v]) continue;
        u = par[i][u];
        v = par[i][v];
      }
      return par[0][u];
    }
  public:
    LowestCommonAncestor(const vector<vector<pair<int,T> > > &G, int root = 0):G(G){
      int n = G.size(), k = 1;
      while ((1<<k) < n) k++;
      par.assign(k, vector<int>(n,-1));
      dist.assign(n,-1);
      weight.assign(n,-1);
      max_w.assign(k, vector<T>(n,-1));
      dfs_(root, -1, 0, 0);
      for (int i = 0; i < k-1; ++i) {
        for (int v = 0; v < n; ++v) {
          if (par[i][v] < 0) par[i+1][v] = -1;
          else {
            par[i+1][v] = par[i][par[i][v]];
            max_w[i+1][v] = max(max_w[i][v], max_w[i][par[i][v]]);
          }
        }
      }
    }
    T get_max_w(int u, int v) {
      int x = query(u,v), k = par.size();
      T res = 0;
      for (int i = 0; i < k; ++i) { //LCAまで移動
        if (((dist[x]-dist[u])>>i) & 1) {
          chmax(res, max_w[i][u]);
          u = par[i][u];
        }
      }
      for (int i = 0; i < k; ++i) {
        if (((dist[x]-dist[v])>>i) & 1) {
          chmax(res, max_w[i][v]);
          v = par[i][v];
        }
      }
      return res;
    }
    int operator()(int u,int v){ return query(u,v);}
    int get_dist(int u, int v){ return dist[u] + dist[v] - 2*dist[query(u,v)];}
    T get_weight(int u, int v){ return weight[u] + weight[v] - 2*weight[query(u,v)];}
    bool is_on_path(int u, int v, int a){ return get_dist(u,a)+get_dist(a,v)==get_dist(u,v);}
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  using P = pair<int,ll>;
  vector<vector<P> > G(n);
  rep(i,n-1) {
    int u,v;
    ll w;
    cin >> u >> v >> w;
    G[u].emplace_back(P(v,w));
    G[v].emplace_back(P(u,w));
  }
  LowestCommonAncestor<ll> LCA(G, 0);
  int q;
  cin >> q;
  rep(i_,q) {
    int x,y,z;
    cin >> x >> y >> z;
    ll res = LCA.get_weight(x,y) + LCA.get_weight(y,z) + LCA.get_weight(z,x);
    res /= 2;
    cout << res << "\n";
  }
  return 0;
}
0