結果

問題 No.898 tri-βutree
ユーザー ShibuyapShibuyap
提出日時 2020-07-26 15:23:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 513 ms / 4,000 ms
コード長 2,967 bytes
コンパイル時間 2,654 ms
コンパイル使用メモリ 206,388 KB
実行使用メモリ 50,560 KB
最終ジャッジ日時 2023-08-08 16:52:21
合計ジャッジ時間 12,418 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 309 ms
50,560 KB
testcase_01 AC 9 ms
34,228 KB
testcase_02 AC 9 ms
34,168 KB
testcase_03 AC 9 ms
34,204 KB
testcase_04 AC 8 ms
34,176 KB
testcase_05 AC 9 ms
34,180 KB
testcase_06 AC 9 ms
34,156 KB
testcase_07 AC 497 ms
44,236 KB
testcase_08 AC 493 ms
44,188 KB
testcase_09 AC 493 ms
44,244 KB
testcase_10 AC 496 ms
44,204 KB
testcase_11 AC 493 ms
44,248 KB
testcase_12 AC 491 ms
44,192 KB
testcase_13 AC 489 ms
44,252 KB
testcase_14 AC 495 ms
44,204 KB
testcase_15 AC 492 ms
44,188 KB
testcase_16 AC 513 ms
44,388 KB
testcase_17 AC 488 ms
44,300 KB
testcase_18 AC 497 ms
44,380 KB
testcase_19 AC 503 ms
44,308 KB
testcase_20 AC 491 ms
44,304 KB
testcase_21 AC 500 ms
44,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("YES");}else{puts("NO");}

struct edge{ ll to, cost; };
#define MAX_N 200005
#define MAX_LOG_N 25
vector<edge> pre_G[MAX_N];
vector<edge> G[MAX_N];
int par_[MAX_N];
int parent[MAX_LOG_N][MAX_N];
int flag_netsukigi[MAX_N];
int depth[MAX_N];
int root;
void make_netsukigi(int r){
    root = r;
    queue<int> que;
    que.push(r);
    flag_netsukigi[r] = 1;
    par_[r] = -1;
    depth[r] = 0;
    while(que.size() > 0){
        int x = que.front();
        que.pop();
        flag_netsukigi[x] = 1;
        rep(i,pre_G[x].size()){
            if(flag_netsukigi[pre_G[x][i].to] == 0){
                par_[pre_G[x][i].to] = x;
                depth[pre_G[x][i].to] = depth[x] + 1;
                G[x].push_back(pre_G[x][i]);
                que.push(pre_G[x][i].to);
            }
        }
    }
}

void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;
    for(int i = 0; i < G[v].size(); i++){
        if(G[v][i].to != p) dfs(G[v][i].to, v, d + 1);
    }
}

void init(int V){
    dfs(root, -1, 0);
    for(int k = 0; k + 1 < MAX_LOG_N; k++){
        for(int v = 0; v < V; v++){
            if(parent[k][v] < 0) parent[k+1][v] = -1;
            else parent[k+1][v] = parent[k][parent[k][v]];
        }
    }
}

int lca(int u, int v){
    if(depth[u] > depth[v])swap(u, v);
    for(int k = 0; k < MAX_LOG_N; k++){
        if((depth[v] - depth[u]) >> k & 1){
            v = parent[k][v];
        }
    }
    if(u == v)return u;
    for(int k = MAX_LOG_N - 1; k >= 0; k--){
        if(parent[k][u] != parent[k][v]){
            u = parent[k][u];
            v = parent[k][v];
        }
    }
    return parent[0][u];
}


ll dist[MAX_N];
void init_dist(int r){
    dist[r] = 0;
    queue<int> que;
    que.push(r);
    while(que.size() > 0){
        int x = que.front();
        que.pop();
        for(int i = 0; i < G[x].size(); i++){
            int y = G[x][i].to;
            dist[y] = dist[x] + G[x][i].cost;
            que.push(y);
        }
    }
}

ll calcLength(int x, int y){
    int w = lca(x, y);
    ll m = dist[x] - dist[w] + dist[y] - dist[w];
    return m;
}

int main() {
    int n;
    cin >> n;

    int a[n-1], b[n-1], c[n-1];

    rep(i,n-1){
        cin >> a[i] >> b[i] >> c[i];
        // a[i]--; b[i]--;
        edge e;
        e.to = b[i]; e.cost = c[i];
        pre_G[a[i]].push_back(e);
        e.to = a[i];
        pre_G[b[i]].push_back(e);
        if(a[i] > b[i])swap(a[i], b[i]);
    }

    make_netsukigi(0);
    init(n);
    init_dist(0);
    

    int q;
    cin >> q;
    rep(_,q){
        int s, t, u;
        cin >> s >> t >> u;
        ll ans = (calcLength(s,t) + calcLength(t,u) + calcLength(u,s)) / 2;
        cout << ans << endl;
    }

    return 0;
}

0