結果

問題 No.898 tri-βutree
ユーザー hipopohipopo
提出日時 2020-04-13 15:05:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 804 ms / 4,000 ms
コード長 3,490 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 148,172 KB
実行使用メモリ 27,932 KB
最終ジャッジ日時 2023-08-08 16:44:20
合計ジャッジ時間 15,014 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 329 ms
27,932 KB
testcase_01 AC 11 ms
15,560 KB
testcase_02 AC 11 ms
15,540 KB
testcase_03 AC 12 ms
15,520 KB
testcase_04 AC 11 ms
15,400 KB
testcase_05 AC 10 ms
15,520 KB
testcase_06 AC 12 ms
15,292 KB
testcase_07 AC 695 ms
24,012 KB
testcase_08 AC 693 ms
23,876 KB
testcase_09 AC 691 ms
24,168 KB
testcase_10 AC 690 ms
23,968 KB
testcase_11 AC 745 ms
23,944 KB
testcase_12 AC 677 ms
23,976 KB
testcase_13 AC 804 ms
23,980 KB
testcase_14 AC 698 ms
24,224 KB
testcase_15 AC 684 ms
24,084 KB
testcase_16 AC 698 ms
23,840 KB
testcase_17 AC 688 ms
24,144 KB
testcase_18 AC 676 ms
24,100 KB
testcase_19 AC 676 ms
24,096 KB
testcase_20 AC 681 ms
23,968 KB
testcase_21 AC 725 ms
24,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

using ll = long long;
using P = pair<int, int>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }

const int MAX_V = 100000;
const int MAX_LOG_V = log2(MAX_V) + 1;

vector<vector<int>> graph(MAX_V);
int root;
vector<vector<int>> parent(MAX_LOG_V, vector<int>(MAX_V + 1));
vector<int> depth(MAX_V + 2);

void dfs(int v, int p, int d) {
    parent.at(0).at(v) = p;
    depth.at(v) = d;
    for(int c: graph.at(v)) if (c != p) dfs(c, v, d + 1);
}

//O(N log N)
void init(int V) {
    dfs(root, -1, 0);
    for (int k = 0; k + 1 < MAX_LOG_V; k++) {
        for (int v = 0; v < V; v++) {
            if (parent.at(k).at(v) < 0) parent.at(k + 1).at(v) = -1;
            else parent.at(k + 1).at(v) = parent.at(k).at(parent.at(k).at(v));
        }
    }
}

//O(log N)
int lca(int u, int v) {
    if (depth.at(u) > depth.at(v)) swap(u, v);
    for (int k = 0; k < MAX_LOG_V; k++) {
        if ((depth.at(v) - depth.at(u)) >> k & 1) v = parent.at(k).at(v);
        if (u == v) return u;
    }

    for (int k = MAX_LOG_V - 1; k >= 0; k--) {
        if (parent.at(k).at(u) != parent.at(k).at(v)) {
            u = parent.at(k).at(u);
            v = parent.at(k).at(v);
        }
    }
    return parent.at(0).at(u);
}

struct Edge {
    int to;
    ll weight;
};

vector<vector<Edge>> edges(MAX_V);
vector<ll> d(MAX_V, -1);

void calc_dist(int v, ll dist, int pre = -1) {
    d[v] = dist;
    for (auto &&e: edges[v]) if (e.to != pre) calc_dist(e.to, dist + e.weight, v);
}

ll get_dist(int u, int v) {
    return d[u] + d[v] - d[lca(u, v)] * 2;
}

int main() {
    int n;
    cin >> n;
    rep(i, n - 1) {
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back(v);
        graph[v].push_back(u);
        edges[u].push_back(Edge{v, w});
        edges[v].push_back(Edge{u, w});
    }
    
    root = 0;
    init(n);
    
    calc_dist(0, 0);
    
    int q;
    cin >> q;
    rep(_q, q) {
        int x, y, z;
        cin >> x >> y >> z;
        int a = lca(lca(x, y), z);
        ll ans = get_dist(a, x) + get_dist(a, y) + get_dist(a, z) - 
            get_dist(a, lca(x, y)) - get_dist(a, lca(x, z)) - get_dist(a, lca(y, z));
        cout << ans << endl;
    }
}
0