結果

問題 No.898 tri-βutree
ユーザー outlineoutline
提出日時 2020-03-17 16:57:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 190 ms / 4,000 ms
コード長 3,609 bytes
コンパイル時間 1,607 ms
コンパイル使用メモリ 124,980 KB
実行使用メモリ 21,592 KB
最終ジャッジ日時 2024-04-26 09:20:32
合計ジャッジ時間 6,670 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
21,592 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 183 ms
17,408 KB
testcase_08 AC 180 ms
17,536 KB
testcase_09 AC 174 ms
17,552 KB
testcase_10 AC 179 ms
17,384 KB
testcase_11 AC 190 ms
17,536 KB
testcase_12 AC 175 ms
17,536 KB
testcase_13 AC 176 ms
17,408 KB
testcase_14 AC 172 ms
17,536 KB
testcase_15 AC 182 ms
17,408 KB
testcase_16 AC 175 ms
17,408 KB
testcase_17 AC 176 ms
17,408 KB
testcase_18 AC 176 ms
17,408 KB
testcase_19 AC 176 ms
17,536 KB
testcase_20 AC 178 ms
17,408 KB
testcase_21 AC 172 ms
17,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932;   // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1e+9 + 7;
// constexpr int mod = 998244353;

struct Edge{
    int to, cost;
    Edge() = default;
    Edge(int to, int cost) : to(to), cost(cost) {}
};

template<typename T>
struct CostLCA{
    using G = vector<vector<Edge>>;

    const int ub_log;
    vector<int> depth;
    vector<T> costs;
    const G& g;
    vector<vector<int>> table;

    CostLCA(const G& g) : g(g), depth(g.size()), costs(g.size()), ub_log(32 - __builtin_clz(g.size())){
        table.assign(ub_log, vector<int>(g.size(), -1));
    }

    void dfs(int from, int par = -1){
        if(par == -1){
            depth[from] = 0;
            costs[from] = 0;
        }
        table[0][from] = par;
        for(int i = 0; i < g[from].size(); ++i){
            int to = g[from][i].to, cost = g[from][i].cost;
            if(to == par)   continue;
            depth[to] = depth[from] + 1;
            costs[to] = costs[from] + cost;
            dfs(to, from);
        }
    }

    void build(int root = 0){
        dfs(root);
        for(int k = 0; k + 1 < ub_log; ++k){
            for(int i = 0; i < table[k].size(); ++i){
                if(table[k][i] == -1)   table[k + 1][i] = -1;
                else    table[k + 1][i] = table[k][table[k][i]];
            }
        }
    }

    int query(int u, int v){
        if(depth[u] > depth[v]) swap(u, v);

        for(int i = ub_log - 1; i >= 0; --i){
            if(((depth[v] - depth[u]) >> i) & 1)    v = table[i][v];
        }
        if(u == v)  return u;

        for(int i = ub_log - 1; i >= 0; --i){
            if(table[i][u] != table[i][v]){
                u = table[i][u];
                v = table[i][v];
            }
        }
        return table[0][u];
    }

    int length(int u, int v){
        int lca = query(u, v);
        return depth[u] + depth[v] - depth[lca] * 2;
    }

    T dist(int u, int v){
        int lca = query(u, v);
        return costs[u] + costs[v] - costs[lca] * 2;
    }

    void print(){
        for(int k = 0; k + 1 < ub_log; ++k){
            for(int i = 0; i < table[k].size(); ++i){
                cerr << table[k][i] << " ";
            }
            cerr << "\n";
        }
    }
};

int n;
vector<vector<Edge>> graph;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    graph.resize(n);
    for(int i = 0; i < n - 1; ++i){
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].emplace_back(Edge(v, w));
        graph[v].emplace_back(Edge(u, w));
    }
    CostLCA<ll> l(graph);
    l.build();
    int q;
    cin >> q;
    for(int i = 0; i < q; ++i){
        int x, y, z;
        cin >> x >> y >> z;
        ll ans = l.dist(x, y) + l.dist(y, z) + l.dist(z, x);
        ans /= 2;
        cout << ans << "\n";
    }
}
0