結果

問題 No.898 tri-βutree
ユーザー yakamotoyakamoto
提出日時 2019-10-04 21:49:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 485 ms / 4,000 ms
コード長 3,836 bytes
コンパイル時間 3,559 ms
コンパイル使用メモリ 179,100 KB
実行使用メモリ 22,988 KB
最終ジャッジ日時 2023-08-08 15:55:11
合計ジャッジ時間 12,116 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
22,988 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 468 ms
17,120 KB
testcase_08 AC 476 ms
16,880 KB
testcase_09 AC 472 ms
16,916 KB
testcase_10 AC 474 ms
17,100 KB
testcase_11 AC 478 ms
16,932 KB
testcase_12 AC 473 ms
16,860 KB
testcase_13 AC 473 ms
16,928 KB
testcase_14 AC 483 ms
16,964 KB
testcase_15 AC 468 ms
16,876 KB
testcase_16 AC 471 ms
16,916 KB
testcase_17 AC 477 ms
16,860 KB
testcase_18 AC 474 ms
16,920 KB
testcase_19 AC 485 ms
17,032 KB
testcase_20 AC 468 ms
17,032 KB
testcase_21 AC 483 ms
17,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#ifndef SOLUTION_COMMON_H

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using PI = pair<int, int>;
template<class T> using V = vector<T>;
using VI = V<int>;
#define _1 first
#define _2 second

#ifdef MY_DEBUG
# define DEBUG(x) x
#else
# define DEBUG(x)
#endif

template<class T>
inline void debug(T &A) {
  DEBUG(
      for (const auto &a : A) {
        cerr << a << " ";
      }
      cerr << '\n';
  )
}

template<class T, class Func>
inline void debug_with_format(T &A, Func f) {
  DEBUG(
      for (const auto &a : A) {
        cerr << f(a) << " ";
      }
      cerr << '\n';
  )
}

template<class T>
inline void debug_dim2(T &A) {
  DEBUG(
      for (const auto &as : A) {
        debug(as);
      }
  )
}

template<typename ... Args>
inline void debug(const char *format, Args const &... args) {
  DEBUG(
      fprintf(stderr, format, args ...);
      cerr << '\n';
  )
}

template<typename ... Args>
string format(const string &fmt, Args ... args) {
  size_t len = snprintf(nullptr, 0, fmt.c_str(), args ...);
  vector<char> buf(len + 1);
  snprintf(&buf[0], len + 1, fmt.c_str(), args ...);
  return string(&buf[0], &buf[0] + len);
}

template<class T1, class T2>
string fmtP(pair<T1, T2> a) {
  stringstream ss;
  ss << "(" << a._1 << "," << a._2 << ")";
  return ss.str();
}

#define SOLUTION_COMMON_H

#endif //SOLUTION_COMMON_H

const int MOD = 1000000007;

class LCA {
public:
  int n;
  V<VI> anc;
  VI depth;
  V<ll> co;
  int k;

  LCA(V<V<PI>> &g, int k, int rt = 0): n((int)g.size()), anc(k, VI(n)), depth(n, -1), co(n), k(k) {
    function<void(int, int, int)> initParent = [&](int v, int p, int c) {
      debug("%d %d", v, p);
      anc[0][v] = p;
      depth[v] = p == -1 ? 0 : depth[p] + 1;
      co[v] = p == -1 ? 0 : co[p] + c;
      for (const auto &u : g[v]) {
        if (depth[u._1] == -1) initParent(u._1, v, u._2);
      }
    };

    initParent(rt, -1, 0);
    for (int kk = 1; kk < k; ++kk) {
      for (int i = 0; i < n; ++i) {
        if (anc[kk - 1][i] == -1) {
          anc[kk][i] = -1;
        } else {
          anc[kk][i] = anc[kk - 1][anc[kk - 1][i]];
        }
      }
    }
  }

  int get_anc(int v, int dist) {
    int i = 0;
    while(dist > 0) {
      if (dist >> i & 1) {
        dist -= 1 << i;
        v = anc[i][v];
      }
      i++;
    }
    return v;
  }

  int operator()(int v, int u) {
    int d = min(depth[v], depth[u]);
    v = get_anc(v, depth[v] - d);
    u = get_anc(u, depth[u] - d);
    if (v == u) return v;

    function<int(int, int, int)> dfs = [&](int v, int u, int s) {
      for (int kk = s; kk >= 0; --kk) {
        if (anc[kk][u] != anc[kk][v]) {
          return dfs(anc[kk][u], anc[kk][v], kk - 1);
        }
      }
      return anc[0][v];
    };

    return dfs(v, u, k - 1);
  }
};

class A {
public:
  void solve(std::istream& in, std::ostream& out) {
    int n;
    in >> n;
    V<V<PI>> g(n);
    for (int i = 0; i < n - 1; ++i) {
      int a, b, c;
      in >> a >> b >> c;
      g[a].emplace_back(b, c);
      g[b].emplace_back(a, c);
    }

    LCA lca(g, 17);

    int q;
    in >> q;
    debug("q:%d", q);
    debug(lca.co);

    auto path = [&](int v, int u) {
      int anc = lca(u, v);
      return lca.co[v] + lca.co[u] - 2 * lca.co[anc];
    };

    for (int i = 0; i < q; ++i) {
      int x, y, z;
      in >> x >> y >> z;
      auto v1 = path(x, y) + path(z, lca(x, y));
      auto v2 = path(y, z) + path(x, lca(y, z));
      auto v3 = path(x, z) + path(y, lca(x, z));
      auto ans = min(v1, min(v2, v3));
      out << ans << '\n';
    }
  }
};


int main() {
	A solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}
0