結果

問題 No.898 tri-βutree
ユーザー kanra824kanra824
提出日時 2019-10-04 21:57:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 517 ms / 4,000 ms
コード長 3,448 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 184,088 KB
実行使用メモリ 26,300 KB
最終ジャッジ日時 2024-04-26 08:33:47
合計ジャッジ時間 12,281 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 317 ms
26,300 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 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 503 ms
23,808 KB
testcase_08 AC 499 ms
23,936 KB
testcase_09 AC 489 ms
23,936 KB
testcase_10 AC 517 ms
23,680 KB
testcase_11 AC 498 ms
23,808 KB
testcase_12 AC 492 ms
23,808 KB
testcase_13 AC 501 ms
23,808 KB
testcase_14 AC 513 ms
23,936 KB
testcase_15 AC 489 ms
23,808 KB
testcase_16 AC 481 ms
23,808 KB
testcase_17 AC 495 ms
23,808 KB
testcase_18 AC 488 ms
23,808 KB
testcase_19 AC 482 ms
23,936 KB
testcase_20 AC 498 ms
23,936 KB
testcase_21 AC 499 ms
23,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template< typename T >
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;
template< typename G >
struct DoublingLowestCommonAncestor {
  const int LOG;
  vector< int > dep;
  const G &g;
  vector< vector< int > > table;
  DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector< int >(g.size(), -1));
  }
  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) dfs(to, idx, d + 1);
    }
  }
  void build() {
    dfs(0, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }
  int query(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = LOG - 1; i >= 0; i--) {
      if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }
};
int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);
  int n; cin >> n;
  WeightedGraph<ll> g(n);
  for(int i=0;i<n-1;++i) {
    int u, v, w; cin >> u >> v >> w;
    g[u].push_back(edge<ll>(u, v, w));
    g[v].push_back(edge<ll>(v, u, w));
  }
  WeightedGraph<ll> tr(n);
  vector<ll> dist(n);
  dist[0] = 0;
  queue<P> que;
  que.push({0, -1});
  while(!que.empty()) {
    int now = que.front().first, prev = que.front().second; que.pop();
    for(int i=0;i<(int)(g[now].size());++i) {
      int next = g[now][i].to;
      ll cost = g[now][i].cost;
      if(next != prev) {
        dist[next] = dist[now] + cost;
        tr[now].push_back(edge<ll>(now, next, cost));
        que.push({next, now});
      }
    }
  }
  DoublingLowestCommonAncestor<WeightedGraph<ll>> lca(tr);
  lca.build();
  int q; cin >> q;
  while(q > 0) {
    q--;
    int x, y, z; cin >> x >> y >> z;
    int p1 = lca.query(x, y), p2 = lca.query(p1, z);
    ll mi = (dist[x] + dist[y] - 2 * dist[p1]) + (dist[p1] + dist[z] - 2 * dist[p2]);
    p1 = lca.query(y, z);
    p2 = lca.query(p1, x);
    mi = min(mi, (dist[y] + dist[z] - 2 * dist[p1]) + (dist[p1] + dist[x] - 2 * dist[p2]));
    p1 = lca.query(z, x);
    p2 = lca.query(p1, y);
    mi = min(mi, (dist[z] + dist[x] - 2 * dist[p1]) + (dist[p1] + dist[y] - 2 * dist[p2]));
    cout << mi << endl;
  }
}
0