結果

問題 No.1094 木登り / Climbing tree
ユーザー ShibuyapShibuyap
提出日時 2020-06-26 23:43:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 931 ms / 2,000 ms
コード長 3,071 bytes
コンパイル時間 3,418 ms
コンパイル使用メモリ 206,676 KB
実行使用メモリ 62,656 KB
最終ジャッジ日時 2023-08-08 00:33:40
合計ジャッジ時間 25,823 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
12,804 KB
testcase_01 AC 858 ms
49,172 KB
testcase_02 AC 233 ms
62,656 KB
testcase_03 AC 234 ms
34,104 KB
testcase_04 AC 214 ms
40,300 KB
testcase_05 AC 315 ms
46,796 KB
testcase_06 AC 483 ms
38,320 KB
testcase_07 AC 931 ms
49,216 KB
testcase_08 AC 889 ms
49,152 KB
testcase_09 AC 928 ms
49,124 KB
testcase_10 AC 888 ms
49,080 KB
testcase_11 AC 913 ms
49,020 KB
testcase_12 AC 929 ms
49,140 KB
testcase_13 AC 913 ms
49,048 KB
testcase_14 AC 925 ms
49,048 KB
testcase_15 AC 511 ms
39,876 KB
testcase_16 AC 717 ms
55,176 KB
testcase_17 AC 597 ms
46,684 KB
testcase_18 AC 570 ms
43,288 KB
testcase_19 AC 688 ms
51,312 KB
testcase_20 AC 898 ms
49,048 KB
testcase_21 AC 626 ms
47,712 KB
testcase_22 AC 918 ms
49,076 KB
testcase_23 AC 906 ms
49,072 KB
testcase_24 AC 897 ms
49,016 KB
testcase_25 AC 913 ms
49,140 KB
testcase_26 AC 893 ms
49,080 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{ int 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];
}


int 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);
        }
    }
}

int calcLength(int x, int y){
    int w = lca(x, y);
    int 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;
        cin >> s >> t;
        s--; t--;
        int ans = calcLength(s,t);
        cout << ans << endl;
    }

    return 0;
}

0