結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
yakamoto
|
| 提出日時 | 2019-10-04 21:49:25 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 502 ms / 4,000 ms |
| コード長 | 3,836 bytes |
| コンパイル時間 | 1,927 ms |
| コンパイル使用メモリ | 179,940 KB |
| 実行使用メモリ | 22,704 KB |
| 最終ジャッジ日時 | 2024-11-08 22:01:06 |
| 合計ジャッジ時間 | 11,765 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
/**
* 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;
}
yakamoto