結果

問題 No.898 tri-βutree
ユーザー alorie10
提出日時 2019-10-05 02:25:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 960 bytes
コンパイル時間 1,731 ms
コンパイル使用メモリ 169,668 KB
実行使用メモリ 29,440 KB
最終ジャッジ日時 2024-11-08 22:45:44
合計ジャッジ時間 12,767 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<bits/stdc++.h>
#define S second
#define F first
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;
ll n,m,a,b,c,dist[100000],parent[100000],depth[100000];
vector<P> v[100000];
void f(ll n,ll m,ll k){
    //cout<<n<<m<<k<<endl;
    dist[n]=m;
    depth[n]=k;
    for(auto x:v[n]){
        if(depth[x.F]!=0)continue;
        parent[x.F]=n;
        f(x.F,m+x.S,k+1);
    }
    return ;
}
ll LCA(ll n,ll m){
    while(depth[n]>depth[m])n=parent[n];
    while(depth[m]>depth[n])m=parent[m];
    while(n!=m){
        n=parent[n];
        m=parent[m];
    }
    return n;
}
ll func(ll a,ll b){
    return dist[a]+dist[b]-dist[LCA(a,b)]*2;
}
int main(void){
    cin>>n;
    for(int i=0;i<n-1;i++){
        cin>>a>>b>>c;
        v[a].push_back({b,c});
        v[b].push_back({a,c});
    }
    f(0,0,1);
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>a>>b>>c;
        cout<<(func(a,b)+func(b,c)+func(c,a))/2<<endl;
    }
}
0