結果

問題 No.1094 木登り / Climbing tree
ユーザー Akidai16Akidai16
提出日時 2021-05-09 06:53:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 926 ms / 2,000 ms
コード長 2,354 bytes
コンパイル時間 3,961 ms
コンパイル使用メモリ 235,252 KB
実行使用メモリ 47,068 KB
最終ジャッジ日時 2024-04-25 19:38:07
合計ジャッジ時間 25,338 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 909 ms
38,596 KB
testcase_02 AC 231 ms
47,068 KB
testcase_03 AC 211 ms
6,944 KB
testcase_04 AC 207 ms
18,560 KB
testcase_05 AC 304 ms
34,128 KB
testcase_06 AC 440 ms
14,336 KB
testcase_07 AC 845 ms
38,732 KB
testcase_08 AC 867 ms
38,764 KB
testcase_09 AC 916 ms
38,900 KB
testcase_10 AC 877 ms
38,716 KB
testcase_11 AC 810 ms
38,604 KB
testcase_12 AC 831 ms
38,668 KB
testcase_13 AC 819 ms
38,784 KB
testcase_14 AC 890 ms
38,904 KB
testcase_15 AC 530 ms
12,928 KB
testcase_16 AC 776 ms
36,456 KB
testcase_17 AC 604 ms
22,912 KB
testcase_18 AC 568 ms
18,048 KB
testcase_19 AC 691 ms
31,068 KB
testcase_20 AC 876 ms
38,624 KB
testcase_21 AC 615 ms
24,472 KB
testcase_22 AC 851 ms
38,776 KB
testcase_23 AC 839 ms
38,596 KB
testcase_24 AC 926 ms
38,804 KB
testcase_25 AC 864 ms
38,704 KB
testcase_26 AC 842 ms
38,624 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