結果
問題 | No.898 tri-βutree |
ユーザー | ooaiu |
提出日時 | 2024-09-03 12:39:25 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 470 ms / 4,000 ms |
コード長 | 4,584 bytes |
コンパイル時間 | 3,755 ms |
コンパイル使用メモリ | 259,488 KB |
実行使用メモリ | 33,652 KB |
最終ジャッジ日時 | 2024-09-03 12:39:40 |
合計ジャッジ時間 | 15,189 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 317 ms
33,652 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 3 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 3 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 467 ms
19,776 KB |
testcase_08 | AC | 461 ms
19,724 KB |
testcase_09 | AC | 425 ms
19,776 KB |
testcase_10 | AC | 459 ms
19,772 KB |
testcase_11 | AC | 429 ms
19,904 KB |
testcase_12 | AC | 429 ms
19,900 KB |
testcase_13 | AC | 461 ms
19,776 KB |
testcase_14 | AC | 434 ms
19,908 KB |
testcase_15 | AC | 470 ms
19,780 KB |
testcase_16 | AC | 431 ms
19,904 KB |
testcase_17 | AC | 440 ms
19,780 KB |
testcase_18 | AC | 459 ms
19,856 KB |
testcase_19 | AC | 426 ms
19,820 KB |
testcase_20 | AC | 433 ms
19,776 KB |
testcase_21 | AC | 441 ms
19,780 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template <typename T> struct graph { struct edge { int from; int to; T cost; }; vector<edge> edges; vector<vector<int>> g; int n; graph(int m) : n(m) { g.resize(n); } virtual int add(int from, int to, T cost) = 0; }; template <typename T> struct tree : graph<T> { using graph<T>::edges; using graph<T>::g; using graph<T>::n; tree(int m) : graph<T>(m) {} int add(int from, int to, T cost = 1) { assert(0 <= from && from < n && 0 <= to && to < n); int id = (int)edges.size(); assert(id < n - 1); g[from].push_back(id); g[to].push_back(id); edges.push_back({from, to, cost}); return id; } }; template <typename T> struct dfs_tree : tree<T> { using tree<T>::edges; using tree<T>::g; using tree<T>::n; vector<int> pv; vector<int> pe; vector<int> order; vector<int> pos; vector<int> end; vector<int> sz; vector<int> root; vector<int> depth; vector<T> dist; dfs_tree(int m) : tree<T>(m) {} void init() { pv = vector<int>(n, -1); pe = vector<int>(n, -1); order.clear(); pos = vector<int>(n, -1); end = vector<int>(n, -1); sz = vector<int>(n, 0); root = vector<int>(n, -1); depth = vector<int>(n, -1); dist = vector<T>(n); } void clear() { pv.clear(); pe.clear(); order.clear(); pos.clear(); end.clear(); sz.clear(); root.clear(); depth.clear(); dist.clear(); } void dfs(int v, bool clear_order = true) { if(pv.empty()) init(); else if(clear_order) order.clear(); dfs_from(v); } private: void _dfs(int v) { pos[v] = (int)order.size(); order.push_back(v); sz[v] = 1; for(int id : g[v]) { if(id == pe[v]) continue; auto &e = edges[id]; int to = e.from ^ e.to ^ v; depth[to] = depth[v] + 1; dist[to] = dist[v] + e.cost; pv[to] = v; pe[to] = id; root[to] = (root[v] != -1 ? root[v] : to); _dfs(to); sz[v] += sz[to]; } end[v] = (int)order.size() - 1; } void dfs_from(int v) { depth[v] = 0; dist[v] = T{}; root[v] = v; pv[v] = pe[v] = -1; _dfs(v); } }; template <typename T> struct lca_tree : dfs_tree<T> { using dfs_tree<T>::edges; using dfs_tree<T>::g; using dfs_tree<T>::n; using dfs_tree<T>::pv; using dfs_tree<T>::pos; using dfs_tree<T>::end; using dfs_tree<T>::depth; int h; vector<vector<int>> pr; lca_tree(int m) : dfs_tree<T>(m) {} inline void build_lca() { assert(!pv.empty()); int max_depth = 0; for(int i = 0; i < n; i++) max_depth = max(max_depth, depth[i]); h = 1; while((1 << h) <= max_depth) h++; pr.resize(n); for(int i = 0; i < n; i++) { pr[i].resize(h); pr[i][0] = pv[i]; } for(int j = 1; j < h; j++) { for(int i = 0; i < n; i++) { pr[i][j] = (pr[i][j - 1] == -1 ? -1 : pr[pr[i][j - 1]][j - 1]); } } } inline bool anc(int x, int y) { return (pos[x] <= pos[y] && end[y] <= end[x]); } inline int go_up(int x, int up) { assert(!pr.empty()); up = min(up, (1 << h) - 1); for(int j = h - 1; j >= 0; j--) { if(up & (1 << j)) { x = pr[x][j]; if(x == -1) break; } } return x; } inline int lca(int x, int y) { assert(!pr.empty()); if(anc(x, y)) return x; if(anc(y, x)) return y; for(int j = h - 1; j >= 0; j--) { if(pr[x][j] != -1 && !anc(pr[x][j], y)) { x = pr[x][j]; } } return pr[x][0]; } }; int main() { int n; cin >> n; lca_tree<long long> g(n); for(int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; g.add(u, v, w); } g.dfs(0); g.build_lca(); int q; cin >> q; auto dist = [&](int x, int y) -> long long { return g.dist[x] + g.dist[y] - 2 * g.dist[g.lca(x, y)]; }; while(q--) { int x, y, z; cin >> x >> y >> z; long long sm = dist(x, y) + dist(y, z) + dist(z, x); cout << sm/2 << endl; } }