結果

問題 No.898 tri-βutree
ユーザー tekihei2317tekihei2317
提出日時 2019-10-07 19:48:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,240 bytes
コンパイル時間 2,405 ms
コンパイル使用メモリ 180,196 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2024-04-22 13:22:41
合計ジャッジ時間 11,712 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define int long long
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

class LowestCommonAncestor
{
public:
    const int N;
    vector<vector<int>> G;
    vector<int> parent,depth;
    const int LOG;
    vector<vector<int>> dp;

    LowestCommonAncestor(vector<vector<int>> G):N(G.size()),G(G),parent(N),depth(N),LOG(32-__builtin_clz(N))
    {
        dfs(0,-1,0);
        dp.assign(LOG,vector<int>(N,-1));
        for(int i=0;i<N;i++) dp[0][i]=parent[i];
        for(int i=0;i+1<LOG;i++){
            for(int j=0;j<N;j++){
                if(dp[i][j]==-1) dp[i+1][j]=-1;
                else dp[i+1][j]=dp[i][dp[i][j]];
            }
        }
    }
    void dfs(int v,int p,int d)
    {
        parent[v]=p;
        depth[v]=d;
        for(int u:G[v]) if(u!=p){
            dfs(u,v,d+1);
        }
    }
    int query(int u,int v)
    {
        if(depth[u]>depth[v]) swap(u,v);
        for(int i=0;i<LOG;i++){
            if((depth[v]-depth[u])>>i&1) v=dp[i][v]; 
        }
        if(u==v) return u;
        for(int i=LOG-1;i>=0;i--){
            if(dp[i][u]!=dp[i][v]){
                u=dp[i][u];
                v=dp[i][v];
            }
        }
        return dp[0][u];
    }
};

struct edge{int to,cost;};

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin>>N;
    vector<vector<edge>> G(N);
    vector<vector<int>> g(N);
    for(int i=0;i<N-1;i++){
        int a,b,c; cin>>a>>b>>c;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vector<int> dist(N);
    function<void(int,int)> dfs=[&](int v,int p)
    {
        for(edge e:G[v]) if(e.to!=p){
            dist[e.to]=dist[v]+e.cost;
            dfs(e.to,v);
        }
    };
    dfs(0,-1);

    LowestCommonAncestor lca(g);
    auto calc=[&](int u,int v)
    {
        return dist[u]+dist[v]-2*lca.query(u,v);
    };

    int Q; cin>>Q;
    while(Q--){
        int x,y,z; cin>>x>>y>>z;
        int ans=calc(x,y)+calc(y,z)+calc(z,x);
        ans/=2;
        cout<<ans<<endl;
    }
    return 0;
}
0