結果

問題 No.1094 木登り / Climbing tree
ユーザー Akidai16Akidai16
提出日時 2021-05-09 06:53:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 970 ms / 2,000 ms
コード長 2,354 bytes
コンパイル時間 4,374 ms
コンパイル使用メモリ 238,360 KB
実行使用メモリ 46,672 KB
最終ジャッジ日時 2023-08-08 01:39:04
合計ジャッジ時間 27,943 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 936 ms
38,408 KB
testcase_02 AC 241 ms
46,672 KB
testcase_03 AC 216 ms
4,900 KB
testcase_04 AC 230 ms
18,324 KB
testcase_05 AC 351 ms
33,764 KB
testcase_06 AC 491 ms
14,376 KB
testcase_07 AC 962 ms
38,520 KB
testcase_08 AC 959 ms
38,580 KB
testcase_09 AC 970 ms
38,588 KB
testcase_10 AC 970 ms
38,648 KB
testcase_11 AC 955 ms
38,444 KB
testcase_12 AC 958 ms
38,584 KB
testcase_13 AC 954 ms
38,444 KB
testcase_14 AC 948 ms
38,456 KB
testcase_15 AC 563 ms
12,552 KB
testcase_16 AC 852 ms
36,148 KB
testcase_17 AC 704 ms
22,772 KB
testcase_18 AC 633 ms
17,832 KB
testcase_19 AC 782 ms
31,068 KB
testcase_20 AC 943 ms
38,792 KB
testcase_21 AC 706 ms
24,140 KB
testcase_22 AC 953 ms
38,448 KB
testcase_23 AC 934 ms
38,400 KB
testcase_24 AC 945 ms
38,724 KB
testcase_25 AC 940 ms
38,504 KB
testcase_26 AC 940 ms
38,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

#define REP(i, init, n) for(int i = (int)(init); i < (int)(n); i++)

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>

int N, Q, digit;
vector<vector<pint>> G;
vvi Parents;
vl dist, depth;

template<class T> void vv_output(vector<vector<T>> A, bool blank = true) {
    for(auto row: A) {
        for(auto a: row) {
            if(blank) cout << a << ' ';
            else cout << a;
        }
        cout << endl;
    }
}

void dfs(int parent) {
    for(auto child: G[parent]) {
        if(child.first == 0 || Parents[child.first][0] >= 0) continue;
        Parents[child.first][0] = parent;
        dist[child.first] = dist[parent] + child.second;
        depth[child.first] = depth[parent] + 1;
        dfs(child.first);
    }
}

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

void solve() {
    digit = 1;
    int n = N;
    while(n > 1) {
        n >>= 1;
        digit++;
    }
    Parents.resize(N, vi(digit, -1));
    dist.resize(N, 0);
    depth.resize(N, 0);
    dfs(0);
    // vv_output(Parents);

    REP(i, 1, digit) {
        REP(j, 0, N) {
            if(Parents[j][i - 1] == -1) continue;
            Parents[j][i] = Parents[Parents[j][i - 1]][i - 1];
        }
    }
    // vv_output(Parents);
    cin >> Q;
    REP(i, 0, Q) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        int parent = lcs(s, t);
        // cout << s << ' ' << t << ' ' << parent << endl;
        cout << dist[s] + dist[t] - 2 * dist[parent] << endl;
    }
}

int main() {
    cin >> N;
    G.resize(N);
    REP(i, 0, N - 1) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    solve();
}
0