結果

問題 No.1094 木登り / Climbing tree
ユーザー warabi0906warabi0906
提出日時 2023-02-07 19:28:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,211 ms / 2,000 ms
コード長 3,506 bytes
コンパイル時間 4,884 ms
コンパイル使用メモリ 241,208 KB
実行使用メモリ 94,600 KB
最終ジャッジ日時 2023-09-19 01:47:59
合計ジャッジ時間 38,508 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1,162 ms
86,696 KB
testcase_02 AC 270 ms
94,600 KB
testcase_03 AC 231 ms
7,340 KB
testcase_04 AC 284 ms
39,460 KB
testcase_05 AC 418 ms
75,776 KB
testcase_06 AC 680 ms
29,208 KB
testcase_07 AC 1,208 ms
86,556 KB
testcase_08 AC 1,211 ms
86,596 KB
testcase_09 AC 1,191 ms
86,860 KB
testcase_10 AC 1,196 ms
86,668 KB
testcase_11 AC 1,183 ms
86,608 KB
testcase_12 AC 1,172 ms
86,556 KB
testcase_13 AC 1,185 ms
86,544 KB
testcase_14 AC 1,179 ms
86,604 KB
testcase_15 AC 577 ms
23,024 KB
testcase_16 AC 801 ms
72,740 KB
testcase_17 AC 691 ms
44,636 KB
testcase_18 AC 642 ms
34,036 KB
testcase_19 AC 759 ms
61,256 KB
testcase_20 AC 1,176 ms
86,632 KB
testcase_21 AC 715 ms
47,428 KB
testcase_22 AC 1,182 ms
86,600 KB
testcase_23 AC 1,165 ms
86,852 KB
testcase_24 AC 1,167 ms
86,600 KB
testcase_25 AC 1,166 ms
86,556 KB
testcase_26 AC 1,157 ms
86,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "atcoder/all"
#define debug cout << "OK" << endl;
template<typename T>
inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
inline int Code(char c) {
	if ('A' <= c and c <= 'Z')return (int)(c - 'A');
	if ('a' <= c and c <= 'z')return (int)(c - 'a');
	if ('0' <= c and c <= '9')return (int)(c - '0');
	assert(false);
}
using namespace std;
using namespace atcoder;
#define int long long
constexpr int MOD = 998244353;
constexpr int INF = (1LL << 62);
using minit = modint998244353;

template<typename T>
ostream& operator << (ostream& st, const vector<T> &v) {
	for (const T value : v) {
		st << value << " ";
	}
	return st;
}
template<typename T>
istream& operator >> (istream& st, vector<T>& v) {
	for (T &value : v) {
		st >> value;
	}
	return st;
}

//
class LCA_C {
public:
    vector<vector<long long>> parent;
    vector<long long> dist;
    vector<long long> distC;
    vector<vector<pair<long long,long long>>> G;
    LCA_C(const vector<vector<pair<long long,long long>>>& gr) { init(gr); }
    void dfs(long long v, long long p, long long d,long long d2) {
        parent[0][v] = p;
        dist[v] = d;
        distC[v] = d2;
        for (pair<long long,long long> e : G[v]) {
            if (e.first == p)continue;
            dfs(e.first, v, d + 1, d2 + e.second);
        }
        return;
    }
    void init(const vector<vector<pair<long long,long long>>>& gr) {
        G = gr;
        parent.assign(33, vector<long long>(G.size(), -1LL));
        dist.assign(G.size(), -1LL);
        distC.assign(G.size(), -1LL);
        dfs(0LL, -1LL, 0LL,0LL);
        for (int i = 0; i < 32; i++) {
            for (int j = 0; j < G.size(); j++) {
                if (parent[i][j] < 0LL) {
                    parent[i + 1][j] = -1LL;
                }
                else {
                    parent[i + 1][j] = parent[i][parent[i][j]];
                }
            }
        }
        return;
    }
    long long lca(long long u, long long v) {
        if (dist[u] < dist[v])swap(u, v);
        long long between = dist[u] - dist[v];
        for (int i = 0; i < 33; i++) {
            if (between & (1LL << i)) { u = parent[i][u]; }
        }
        if (u == v)return u;
        for (int i = 32; i >= 0; i--) {
            if (parent[i][u] != parent[i][v]) {
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        assert(parent[0][u] == parent[0][v]);
        return parent[0][u];
    }
    long long get_dist(long long u, long long v) {
        return distC[u] + distC[v] - 2 * distC[lca(u, v)];
    }
    bool on_path(long long u, long long v, long long a) {
        return get_dist(u, v) == get_dist(u, a) + get_dist(v, a);
    }
};
//

void solve() {
    int N;
    cin >> N;
    vector<vector<pair<int, int>>> G(N);
    for (int i = 0; i < N - 1; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        u--; v--;
        G[u].push_back({ v, c });
        G[v].push_back({ u, c });
    }
    LCA_C C(G);
    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        cout << C.get_dist(a, b) << endl;
    }
    return;
}
signed main() {
	int T = 1;
	// cin >> T;
	for (int i = 0; i < T; i++)
		solve();
	return 0;
}

/*
* わらびのコードこーどすぺーす
 (σ・∀・)σゲッツ!!
*/
0