結果
問題 | No.898 tri-βutree |
ユーザー | yuji9511 |
提出日時 | 2019-10-04 22:57:16 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 514 ms / 4,000 ms |
コード長 | 3,484 bytes |
コンパイル時間 | 1,969 ms |
コンパイル使用メモリ | 166,064 KB |
実行使用メモリ | 52,096 KB |
最終ジャッジ日時 | 2024-11-08 22:22:26 |
合計ジャッジ時間 | 12,176 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 215 ms
52,096 KB |
testcase_01 | AC | 33 ms
44,032 KB |
testcase_02 | AC | 33 ms
44,032 KB |
testcase_03 | AC | 33 ms
44,032 KB |
testcase_04 | AC | 32 ms
44,032 KB |
testcase_05 | AC | 32 ms
44,160 KB |
testcase_06 | AC | 33 ms
44,160 KB |
testcase_07 | AC | 489 ms
49,152 KB |
testcase_08 | AC | 494 ms
49,152 KB |
testcase_09 | AC | 495 ms
49,280 KB |
testcase_10 | AC | 488 ms
49,152 KB |
testcase_11 | AC | 513 ms
49,280 KB |
testcase_12 | AC | 488 ms
49,152 KB |
testcase_13 | AC | 497 ms
49,152 KB |
testcase_14 | AC | 514 ms
49,280 KB |
testcase_15 | AC | 502 ms
49,152 KB |
testcase_16 | AC | 513 ms
49,152 KB |
testcase_17 | AC | 487 ms
49,280 KB |
testcase_18 | AC | 478 ms
49,152 KB |
testcase_19 | AC | 470 ms
49,152 KB |
testcase_20 | AC | 472 ms
49,152 KB |
testcase_21 | AC | 475 ms
49,280 KB |
ソースコード
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> lpair; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} struct Tree { private: ll N, logN; vector<lpair> edge[200010]; ll depth[200010] = {}; ll sum[200010] = {}; ll parent[200010] = {}; ll next_val[20][200010] = {}; public: Tree(){ } void init(ll n){ N = n; logN = floor(log2(N)) + 1; } ll getDepth(ll x) { return depth[x]; } ll getSum(ll x) { return sum[x]; } void add(ll x, ll y, ll z){ edge[x].push_back({y,z}); } void prepare(){ dfs_parent(); dfs_sum(); makeDoubling(); } void dfs_parent(ll cur = 0, ll par = -1, ll d = 0){ depth[cur] = d; parent[cur] = par; for(auto &e: edge[cur]){ if(e.first == par) continue; dfs_parent(e.first, cur, d+1); } } void dfs_sum(ll cur = 0, ll par = -1, ll d = 0){ if(par != -1) sum[cur] = sum[par] + d; for(auto &e: edge[cur]){ if(e.first == par) continue; dfs_sum(e.first, cur, e.second); } } void makeDoubling(){ rep(i,0,N) next_val[0][i] = parent[i]; rep(k,0,logN){ rep(i,0,N){ if(next_val[k][i] == -1) next_val[k+1][i] = -1; else next_val[k+1][i] = next_val[k][next_val[k][i]]; } } } ll getParent(ll cur, ll num) { ll p = cur; rrep(k,logN-1, 0){ if(p == -1) break; if((num >> k) & 1) p = next_val[k][p]; } return p; } ll getLCA(ll va, ll vb){ if(depth[va] > depth[vb]){ va = getParent(va, depth[va] - depth[vb]); }else if(depth[va] < depth[vb]){ vb = getParent(vb, depth[vb] - depth[va]); } ll lv = -1, rv = N+1; while(rv - lv > 1){ ll mid = (rv + lv) / 2; ll ta = getParent(va, mid); ll tb = getParent(vb, mid); if(ta == -1 || tb == -1){ rv = mid; }else if(ta != tb){ lv = mid; }else{ rv = mid; } } return getParent(va, rv); } } tree; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; tree.init(N); rep(i,0,N-1){ ll u,v,w; cin >> u >> v >> w; tree.add(u,v,w); tree.add(v,u,w); } tree.prepare(); ll Q; cin >> Q; while(Q--){ ll x,y,z; cin >> x >> y >> z; ll ans = tree.getSum(x) + tree.getSum(y) + tree.getSum(z); ll v1 = tree.getLCA(x,y); ll v2 = tree.getLCA(y,z); ll v3 = tree.getLCA(x,z); ans -= (tree.getSum(v1) + tree.getSum(v2) + tree.getSum(v3)); print(ans); } }