結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | outline |
提出日時 | 2020-12-27 20:22:30 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 298 ms / 2,000 ms |
コード長 | 3,389 bytes |
コンパイル時間 | 2,045 ms |
コンパイル使用メモリ | 145,144 KB |
実行使用メモリ | 36,824 KB |
最終ジャッジ日時 | 2024-04-25 19:33:19 |
合計ジャッジ時間 | 10,592 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 284 ms
31,524 KB |
testcase_02 | AC | 91 ms
36,824 KB |
testcase_03 | AC | 37 ms
6,940 KB |
testcase_04 | AC | 71 ms
15,104 KB |
testcase_05 | AC | 131 ms
27,948 KB |
testcase_06 | AC | 109 ms
11,648 KB |
testcase_07 | AC | 296 ms
31,528 KB |
testcase_08 | AC | 275 ms
31,668 KB |
testcase_09 | AC | 285 ms
31,584 KB |
testcase_10 | AC | 293 ms
31,628 KB |
testcase_11 | AC | 277 ms
31,588 KB |
testcase_12 | AC | 278 ms
31,592 KB |
testcase_13 | AC | 286 ms
31,500 KB |
testcase_14 | AC | 270 ms
31,628 KB |
testcase_15 | AC | 95 ms
10,204 KB |
testcase_16 | AC | 206 ms
28,776 KB |
testcase_17 | AC | 139 ms
18,068 KB |
testcase_18 | AC | 117 ms
14,304 KB |
testcase_19 | AC | 168 ms
24,100 KB |
testcase_20 | AC | 298 ms
31,752 KB |
testcase_21 | AC | 145 ms
19,148 KB |
testcase_22 | AC | 277 ms
31,612 KB |
testcase_23 | AC | 285 ms
31,692 KB |
testcase_24 | AC | 277 ms
31,600 KB |
testcase_25 | AC | 271 ms
31,564 KB |
testcase_26 | AC | 284 ms
31,720 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <queue> #include <string> #include <map> #include <set> #include <stack> #include <tuple> #include <deque> #include <array> #include <numeric> #include <bitset> #include <iomanip> #include <cassert> #include <chrono> #include <random> #include <limits> #include <iterator> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <cstring> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; using P = pair<int, int>; constexpr int INF = 1001001001; // constexpr int mod = 1000000007; constexpr int mod = 998244353; template<class T> inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template<class T> inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template<typename T> struct Edge{ int to; T cost; Edge() = default; Edge(int to, T cost) : to(to), cost(cost) {} }; template<typename T> struct CostLCA{ using G = vector<vector<Edge<T>>>; const int ub_log; vector<int> depth; vector<T> costs; const G& g; vector<vector<int>> table; CostLCA(const G& g) : g(g), depth(g.size()), costs(g.size()), ub_log(32 - __builtin_clz(g.size())){ table.assign(ub_log, vector<int>(g.size(), -1)); } void dfs(int from, int par = -1, int dep = 0, T sum = 0){ table[0][from] = par; depth[from] = dep; costs[from] = sum; for(int i = 0; i < (int)g[from].size(); ++i){ int to = g[from][i].to; T cost = g[from][i].cost; if(to != par) dfs(to, from, dep + 1, sum + cost); } } void build(int root = 0){ dfs(root); for(int k = 0; k + 1 < ub_log; ++k){ for(int i = 0; i < (int)table[k].size(); ++i){ if(table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v){ if(depth[u] > depth[v]) swap(u, v); v = get(v, depth[v] - depth[u]); if(u == v) return u; for(int i = ub_log - 1; i >= 0; --i){ if(table[i][u] != table[i][v]){ u = table[i][u]; v = table[i][v]; } } return table[0][u]; } int get(int v, int x){ if(x <= 0) return v; for(int i = ub_log - 1; i >= 0; --i){ if(x >> i & 1) v = table[i][v]; } return v; } int length(int u, int v){ int lca = query(u, v); return depth[u] + depth[v] - depth[lca] * 2; } T dist(int u, int v){ int lca = query(u, v); return costs[u] + costs[v] - costs[lca] * 2; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N; vector<vector<Edge<int>>> g(N); for(int i = 1; i < N; ++i){ int a, b, c; cin >> a >> b >> c; --a, --b; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } CostLCA<int> lca(g); lca.build(); cin >> Q; for(int q = 0; q < Q; ++q){ int s, t; cin >> s >> t; --s, --t; cout << lca.dist(s, t) << '\n'; } return 0; }