結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | face4 |
提出日時 | 2020-07-05 14:05:50 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 918 ms / 2,000 ms |
コード長 | 2,598 bytes |
コンパイル時間 | 1,081 ms |
コンパイル使用メモリ | 81,776 KB |
実行使用メモリ | 58,752 KB |
最終ジャッジ日時 | 2024-04-25 19:18:22 |
合計ジャッジ時間 | 22,652 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
8,944 KB |
testcase_01 | AC | 908 ms
44,276 KB |
testcase_02 | AC | 242 ms
58,752 KB |
testcase_03 | AC | 210 ms
10,356 KB |
testcase_04 | AC | 218 ms
23,900 KB |
testcase_05 | AC | 353 ms
39,884 KB |
testcase_06 | AC | 454 ms
19,336 KB |
testcase_07 | AC | 902 ms
44,148 KB |
testcase_08 | AC | 905 ms
44,272 KB |
testcase_09 | AC | 907 ms
44,144 KB |
testcase_10 | AC | 886 ms
44,272 KB |
testcase_11 | AC | 885 ms
44,152 KB |
testcase_12 | AC | 894 ms
44,148 KB |
testcase_13 | AC | 874 ms
44,140 KB |
testcase_14 | AC | 896 ms
44,144 KB |
testcase_15 | AC | 484 ms
19,488 KB |
testcase_16 | AC | 690 ms
47,084 KB |
testcase_17 | AC | 592 ms
31,104 KB |
testcase_18 | AC | 522 ms
25,684 KB |
testcase_19 | AC | 626 ms
39,880 KB |
testcase_20 | AC | 890 ms
44,280 KB |
testcase_21 | AC | 578 ms
32,620 KB |
testcase_22 | AC | 918 ms
44,092 KB |
testcase_23 | AC | 887 ms
44,148 KB |
testcase_24 | AC | 882 ms
44,276 KB |
testcase_25 | AC | 869 ms
44,148 KB |
testcase_26 | AC | 873 ms
44,140 KB |
ソースコード
#include<iostream> #include<vector> using namespace std; typedef pair<int,int> pii; struct LCA{ int n, bit; // n < 1<<bit vector<int> depth; vector<vector<int>> par, v; LCA(int n) : n(n){ depth.resize(n, -1); bit = 0; while(1<<bit <= n) bit++; par.resize(bit); for(int i = 0; i < bit; i++) par[i].resize(n); v.resize(n); } void add_edge(int a, int b){ v[a].push_back(b); v[b].push_back(a); } void dfs(int now, int p, int d){ par[0][now] = p; depth[now] = d; for(int child : v[now]){ if(child == p) continue; dfs(child, now, d+1); } } void build(){ for(int i = 0; i < n; i++){ if(depth[i] == -1) dfs(i, -1, 0); } for(int k = 0; k+1 < bit; k++){ for(int i = 0; i < n; i++){ if(par[k][i] < 0) par[k+1][i] = -1; else par[k+1][i] = par[k][par[k][i]]; } } } int lca(int a, int b){ // make b deeper if(depth[a] > depth[b]) swap(a, b); for(int k = 0; k < 30; k++){ // depth(b)-depth(a) is equal to or bigger than 2^k if(((depth[b]-depth[a])>>k)&1){ b = par[k][b]; } } if(a == b) return a; for(int k = bit-1; k >= 0; k--){ if(par[k][a] != par[k][b]){ a = par[k][a]; b = par[k][b]; } } return par[0][a]; } int dist(int a, int b){ int l = lca(a, b); return depth[a]-depth[l] + depth[b]-depth[l]; } }; vector<pii> v[200000]; vector<int> ind(200000), val; int k = 0; void dfs(int x, int p){ ind[x] = k; for(pii edge : v[x]){ int to = edge.first, cost = edge.second; if(to == p) continue; val.push_back(cost); k++; dfs(to, x); val.push_back(-cost); k++; } } int main(){ int n; cin >> n; LCA l(n); for(int i = 0; i < n-1; i++){ int a, b, c; cin >> a >> b >> c; a--, b--; v[a].push_back({b, c}); v[b].push_back({a, c}); l.add_edge(a, b); } l.build(); val.push_back(0); dfs(0, -1); for(int i = 1; i < val.size(); i++) val[i] += val[i-1]; int q; cin >> q; while(q--){ int s, t; cin >> s >> t; s--, t--; int lca = l.lca(s, t); cout << val[ind[s]]-val[ind[lca]] + val[ind[t]]-val[ind[lca]] << endl; } return 0; }