結果
問題 | No.898 tri-βutree |
ユーザー | erbowl |
提出日時 | 2020-04-25 16:46:55 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 556 ms / 4,000 ms |
コード長 | 3,813 bytes |
コンパイル時間 | 2,815 ms |
コンパイル使用メモリ | 218,028 KB |
実行使用メモリ | 33,068 KB |
最終ジャッジ日時 | 2024-11-08 23:36:45 |
合計ジャッジ時間 | 13,752 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 325 ms
33,068 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 547 ms
24,148 KB |
testcase_08 | AC | 545 ms
24,272 KB |
testcase_09 | AC | 529 ms
24,272 KB |
testcase_10 | AC | 549 ms
24,144 KB |
testcase_11 | AC | 549 ms
24,144 KB |
testcase_12 | AC | 550 ms
24,136 KB |
testcase_13 | AC | 546 ms
24,264 KB |
testcase_14 | AC | 542 ms
24,148 KB |
testcase_15 | AC | 554 ms
24,144 KB |
testcase_16 | AC | 547 ms
24,132 KB |
testcase_17 | AC | 556 ms
24,144 KB |
testcase_18 | AC | 555 ms
24,140 KB |
testcase_19 | AC | 548 ms
24,268 KB |
testcase_20 | AC | 537 ms
24,148 KB |
testcase_21 | AC | 530 ms
24,136 KB |
ソースコード
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'; } }