結果

問題 No.898 tri-βutree
ユーザー stoqstoq
提出日時 2020-03-31 05:55:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 4,000 ms
コード長 3,737 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 175,884 KB
実行使用メモリ 45,844 KB
最終ジャッジ日時 2023-08-08 16:43:35
合計ジャッジ時間 7,536 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
45,844 KB
testcase_01 AC 31 ms
35,344 KB
testcase_02 AC 28 ms
35,516 KB
testcase_03 AC 30 ms
35,352 KB
testcase_04 AC 30 ms
35,404 KB
testcase_05 AC 30 ms
35,604 KB
testcase_06 AC 29 ms
35,376 KB
testcase_07 AC 202 ms
40,528 KB
testcase_08 AC 195 ms
40,508 KB
testcase_09 AC 201 ms
40,744 KB
testcase_10 AC 196 ms
40,484 KB
testcase_11 AC 206 ms
40,516 KB
testcase_12 AC 199 ms
40,508 KB
testcase_13 AC 202 ms
40,564 KB
testcase_14 AC 202 ms
40,524 KB
testcase_15 AC 202 ms
40,640 KB
testcase_16 AC 193 ms
40,472 KB
testcase_17 AC 194 ms
40,556 KB
testcase_18 AC 194 ms
40,516 KB
testcase_19 AC 207 ms
40,620 KB
testcase_20 AC 207 ms
40,528 KB
testcase_21 AC 202 ms
40,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

const int MOD_TYPE = 1;
const ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ld PI = acos(-1.0);
const ld EPS = 1e-11;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;

vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

struct Tree
{
  const static int MAX_V = 200010;
  using P = pair<int, ll>;
  vector<P> E[MAX_V];
  int par[MAX_V];
  vector<int> ch[MAX_V];
  int depth[MAX_V];
  ll dist[MAX_V];

  void add_E(int a, int b, ll w = 1, bool direction = false)
  {
    E[a].push_back(MP(b, w));
    if (!direction)
      E[b].push_back(MP(a, w));
  }

  void dfs(int p, int d, ll w, bool make_ch)
  {
    for (auto pi : E[p])
    {
      int v = pi.first;
      if (par[v] != -2)
        continue;
      par[v] = p;
      depth[v] = d + 1;
      dist[v] = w + pi.second;
      if (make_ch)
        ch[p].push_back(v);
      dfs(v, d + 1, w + pi.second, make_ch);
    }
  }

  void make_tree(int root = 0, bool make_ch = false)
  {
    fill(par, par + MAX_V, -2);
    par[root] = -1;
    depth[root] = dist[root] = 0;
    dfs(root, 0, 0, make_ch);
  }

  int par_double[MAX_V][25]; //頂点iの2^k個上の頂点
  bool calculated = false;
  void calc_double()
  {
    for (int i = 0; i < MAX_V; i++)
      par_double[i][0] = (par[i] < 0 ? -1 : par[i]);

    for (int k = 0; k < 24; k++)
    {
      for (int i = 0; i < MAX_V; i++)
      {
        if (par_double[i][k] == -1)
          par_double[i][k + 1] = -1;
        else
          par_double[i][k + 1] = par_double[par_double[i][k]][k];
      }
    }
  }

  int getLCA(int a, int b)
  {
    if (!calculated)
    {
      calc_double();
      calculated = true;
    }
    if (a == b)
      return a;
    if (depth[a] < depth[b])
      swap(a, b);
    for (int k = 24; k >= 0; k--)
    {
      if (par_double[a][k] != -1 && depth[par_double[a][k]] >= depth[b])
      {
        a = par_double[a][k];
      }
    }
    if (a == b)
      return a;
    for (int k = 24; k >= 0; k--)
    {
      if (par_double[a][k] != -1 && par_double[a][k] != par_double[b][k])
      {
        a = par_double[a][k];
        b = par_double[b][k];
      }
    }
    return par_double[a][0];
  }

  inline ll length(int a, int b)
  {
    return depth[a] + depth[b] - 2 * depth[getLCA(a, b)];
  }

  inline ll distance(int a, int b)
  {
    return dist[a] + dist[b] - 2 * dist[getLCA(a, b)];
  }
};

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(30) << setiosflags(ios::fixed);

  int n;
  cin >> n;
  Tree tr;
  rep(i, n - 1)
  {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    tr.add_E(u, v, w);
  }
  tr.make_tree(0);
  int q;
  cin >> q;
  rep(qi, q)
  {
    int x, y, z;
    cin >> x >> y >> z;

    cout << (tr.distance(x, y) + tr.distance(y, z) + tr.distance(z, x)) / 2 << "\n";
  }

  return 0;
}
0