結果

問題 No.898 tri-βutree
ユーザー outlineoutline
提出日時 2020-03-17 16:11:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,425 bytes
コンパイル時間 3,688 ms
コンパイル使用メモリ 125,828 KB
実行使用メモリ 27,040 KB
最終ジャッジ日時 2023-08-20 18:50:48
合計ジャッジ時間 10,003 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
27,040 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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) {}
};

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

    vector<int> depth;
    vector<vector<int>> table;
    vector<ll> costs; 
    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, 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];
    }

    int 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";
        }
    }
};

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));
    }
    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