結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
totori_nyaa
|
| 提出日時 | 2020-02-08 03:24:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,999 bytes |
| コンパイル時間 | 1,563 ms |
| コンパイル使用メモリ | 174,096 KB |
| 実行使用メモリ | 30,260 KB |
| 最終ジャッジ日時 | 2024-09-25 08:37:59 |
| 合計ジャッジ時間 | 7,211 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 1 WA * 20 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// LCA
// verify: https://atcoder.jp/contests/abc014/submissions/9535352
vector<vector<array<ll,2>>> v(200010);
bool used[200010];
int depth[200010];
int par[200010][20];
ll dp[200020];
// 根からの深さと1つ上の親を求める. initの中で使う
void dfs(int p,int d,ll cost){
used[p] = 1;
depth[p] = d;
dp[p]=cost;
for(auto i:v[p]){
if(!used[i[0]]){
dfs(i[0],d+1,cost+i[1]);
par[i[0]][0] = p;
}
}
}
// init(n, root) でダブリングテーブルを作製する, nは頂点数
void init(int n,int root=0){
for(int i=0;i<n;i++){
used[i]=false;
for(int j=0;j<20;j++){
par[i][j] = -1;
}
}
dfs(root,0,0);
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1] == -1) continue;
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
//頂点aのd個上の親を求める
ll get_nyaa(ll a,ll d){
for(ll i=19;i>=0;i--){
if(d & (1<<i)) a = par[a][i];
}
return a;
}
// a,bのLCAを求める
int lca(int a,int b){
if(depth[a] < depth[b]) swap(a,b);
int dif = depth[a] - depth[b];
for(int i=19;i>=0;i--){
if(dif & (1<<i)) a = par[a][i];
}
if(a==b) return a;
for(int i=19;i>=0;i--){
if(par[a][i]!=par[b][i]){
a = par[a][i], b = par[b][i];
}
}
return par[a][0];
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n;
cin>>n;
for(int i=1;i<n;i++){
ll a,b,c;
cin>>a>>b>>c;
v[b].push_back({a,c});
v[a].push_back({b,c});
}
init(n);
int q; cin>>q;
while(q--){
int a,b,c;
cin>>a>>b>>c;
ll ans = dp[a] + dp[b] + dp[c];
ans -= dp[lca(a,b)] + dp[lca(a,c)] + dp[lca(b,c)];
ans += dp[lca(a,lca(b,c))];
cout << ans << "\n";
}
}
totori_nyaa