結果

問題 No.898 tri-βutree
ユーザー alorie10alorie10
提出日時 2019-10-05 02:25:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 960 bytes
コンパイル時間 1,952 ms
コンパイル使用メモリ 168,340 KB
実行使用メモリ 26,048 KB
最終ジャッジ日時 2023-08-08 16:13:56
合計ジャッジ時間 12,734 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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