結果

問題 No.1094 木登り / Climbing tree
ユーザー ShibuyapShibuyap
提出日時 2020-06-26 23:43:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,019 ms / 2,000 ms
コード長 3,071 bytes
コンパイル時間 2,459 ms
コンパイル使用メモリ 204,724 KB
実行使用メモリ 63,020 KB
最終ジャッジ日時 2024-04-25 18:37:45
合計ジャッジ時間 26,554 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
33,920 KB
testcase_01 AC 969 ms
49,204 KB
testcase_02 AC 243 ms
63,020 KB
testcase_03 AC 234 ms
35,600 KB
testcase_04 AC 248 ms
41,008 KB
testcase_05 AC 381 ms
47,352 KB
testcase_06 AC 540 ms
39,640 KB
testcase_07 AC 998 ms
49,264 KB
testcase_08 AC 1,008 ms
49,332 KB
testcase_09 AC 995 ms
49,368 KB
testcase_10 AC 982 ms
49,352 KB
testcase_11 AC 985 ms
49,392 KB
testcase_12 AC 981 ms
49,252 KB
testcase_13 AC 992 ms
49,512 KB
testcase_14 AC 982 ms
49,320 KB
testcase_15 AC 567 ms
40,780 KB
testcase_16 AC 799 ms
55,472 KB
testcase_17 AC 678 ms
47,220 KB
testcase_18 AC 631 ms
43,740 KB
testcase_19 AC 754 ms
51,676 KB
testcase_20 AC 981 ms
49,312 KB
testcase_21 AC 711 ms
48,360 KB
testcase_22 AC 1,019 ms
49,384 KB
testcase_23 AC 974 ms
49,272 KB
testcase_24 AC 992 ms
49,164 KB
testcase_25 AC 975 ms
49,356 KB
testcase_26 AC 980 ms
49,432 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