結果

問題 No.898 tri-βutree
ユーザー outlineoutline
提出日時 2020-03-17 16:36:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 4,000 ms
コード長 3,575 bytes
コンパイル時間 1,523 ms
コンパイル使用メモリ 125,496 KB
実行使用メモリ 23,184 KB
最終ジャッジ日時 2023-08-08 16:41:31
合計ジャッジ時間 6,456 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
23,184 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 176 ms
18,568 KB
testcase_08 AC 175 ms
18,928 KB
testcase_09 AC 177 ms
18,928 KB
testcase_10 AC 176 ms
18,756 KB
testcase_11 AC 178 ms
18,928 KB
testcase_12 AC 178 ms
18,556 KB
testcase_13 AC 179 ms
19,024 KB
testcase_14 AC 178 ms
18,840 KB
testcase_15 AC 176 ms
19,020 KB
testcase_16 AC 175 ms
18,760 KB
testcase_17 AC 176 ms
18,560 KB
testcase_18 AC 177 ms
18,932 KB
testcase_19 AC 178 ms
18,896 KB
testcase_20 AC 174 ms
18,864 KB
testcase_21 AC 174 ms
18,560 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;
    ll cost;
    edge() = default;
    edge(int to, ll cost) : to(to), cost(cost) {}
};

struct LCA{
    using G = vector<vector<edge>>;

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

    void dfs(int from = 0, int p = -1){
        if(p == -1){
            depth[from] = 0;
            costs[from] = 0;
        }
        table[0][from] = p;

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

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

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

    int lca(int u, int v){
        if(depth[u] > depth[v]) swap(u, v);
        for(int k = ub_log - 1; k >= 0; --k){
            if(((depth[v] - depth[u]) >> k) & 1)    v = table[k][v];
        }
        if(u == v)  return u;
        for(int k = ub_log - 1; k >= 0; --k){
            if(table[k][u] != table[k][v]){
                u = table[k][u];
                v = table[k][v];
            }
        }
        return table[0][u];
    }

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

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

    void cprint(){
        for(int i = 0; i < g.size(); ++i)   cerr << costs[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;
        ll w;
        cin >> u >> v >> w;
        graph[u].emplace_back(edge(v, w));
        graph[v].emplace_back(edge(u, w));
    }
    LCA l(graph);
    int q;
    cin >> q;
    for(int i = 0; i < q; ++i){
        int x, y, z;
        cin >> x >> y >> z;
        ll ans = l.get_dist(x, y) + l.get_dist(y, z) + l.get_dist(z, x);
        ans /= 2;
        cout << ans << "\n";
    }
}
0