結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-25 16:46:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 644 ms / 4,000 ms |
| コード長 | 3,813 bytes |
| コンパイル時間 | 2,832 ms |
| コンパイル使用メモリ | 209,988 KB |
| 最終ジャッジ日時 | 2025-01-10 01:11:58 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template<class Monoid> struct SegTree {
using Func = function<Monoid(Monoid, Monoid)>;
const Func F;
const Monoid UNITY;
int SIZE_R;
vector<Monoid> dat;
SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
void init(int n) {
SIZE_R = 1;
while (SIZE_R < n) SIZE_R *= 2;
dat.assign(SIZE_R * 2, UNITY);
}
/* set, a is 0-indexed */
void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
void build() {
for (int k = SIZE_R - 1; k > 0; --k)
dat[k] = F(dat[k*2], dat[k*2+1]);
}
/* update a, a is 0-indexed */
void update(int a, const Monoid &v) {
int k = a + SIZE_R;
dat[k] = v;
while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
}
/* get [a, b), a and b are 0-indexed */
Monoid get(int a, int b) {
Monoid vleft = UNITY, vright = UNITY;
for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
if (left & 1) vleft = F(vleft, dat[left++]);
if (right & 1) vright = F(dat[--right], vright);
}
return F(vleft, vright);
}
inline Monoid operator [] (int a) { return dat[a + SIZE_R]; }
/* debug */
void print() {
for (int i = 0; i < SIZE_R; ++i) {
cout << (*this)[i];
if (i != SIZE_R-1) cout << ",";
}
cout << endl;
}
};
using Graph = vector<vector<int> >;
struct LCA {
vector<vector<int> > parent; // parent[d][v] := 2^d-th parent of v
vector<int> depth;
LCA() { }
LCA(const Graph &G, int r = 0) { init(G, r); }
void init(const Graph &G, int r = 0) {
int V = (int)G.size();
int h = 1;
while ((1<<h) < V) ++h;
parent.assign(h, vector<int>(V, -1));
depth.assign(V, -1);
dfs(G, r, -1, 0);
for (int i = 0; i+1 < (int)parent.size(); ++i)
for (int v = 0; v < V; ++v)
if (parent[i][v] != -1)
parent[i+1][v] = parent[i][parent[i][v]];
}
void dfs(const Graph &G, int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
for (auto e : G[v]) if (e != p) dfs(G, e, v, d+1);
}
int get(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
for (int i = 0; i < (int)parent.size(); ++i)
if ( (depth[v] - depth[u]) & (1<<i) )
v = parent[i][v];
if (u == v) return u;
for (int i = (int)parent.size()-1; i >= 0; --i) {
if (parent[i][u] != parent[i][v]) {
u = parent[i][u];
v = parent[i][v];
}
}
return parent[0][u];
}
};
int main() {
ll n;
std::cin >> n;
vector<vector<pair<ll,ll>>> edges(n);
Graph G(n);
for (int i = 0; i < n-1; i++) {
ll a,b,c;
std::cin >> a>>b>>c;
edges[b].push_back({a,c});
edges[a].push_back({b,c});
G[a].push_back(b);
G[b].push_back(a);
}
LCA lca(G);
vector<ll> dist(n,0);
function<void(int, int)> rec = [&] (int par, int cur) {
for (auto e : edges[cur]) {
if(e.first==par)continue;
dist[e.first] = dist[cur]+e.second;
rec(cur, e.first);
}
};
rec(-1,0);
ll q;
std::cin >> q;
for (int i = 0; i < q; i++) {
ll x,y,z;
std::cin >> x>>y>>z;
cout<<dist[x]+dist[y]+dist[z]-(dist[lca.get(x,y)]+dist[lca.get(z,y)]+dist[lca.get(x,z)])
<<'\n';
}
}