結果

問題 No.898 tri-βutree
ユーザー ganariyaganariya
提出日時 2019-10-05 11:51:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,302 ms / 4,000 ms
コード長 5,357 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 159,500 KB
実行使用メモリ 39,100 KB
最終ジャッジ日時 2024-04-26 08:53:03
合計ジャッジ時間 23,966 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 456 ms
39,100 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1,275 ms
36,224 KB
testcase_08 AC 1,273 ms
36,224 KB
testcase_09 AC 1,298 ms
36,224 KB
testcase_10 AC 1,302 ms
36,224 KB
testcase_11 AC 1,287 ms
36,224 KB
testcase_12 AC 1,302 ms
36,120 KB
testcase_13 AC 1,278 ms
36,176 KB
testcase_14 AC 1,282 ms
36,352 KB
testcase_15 AC 1,243 ms
36,352 KB
testcase_16 AC 1,265 ms
36,096 KB
testcase_17 AC 1,281 ms
36,204 KB
testcase_18 AC 1,276 ms
36,188 KB
testcase_19 AC 1,271 ms
36,476 KB
testcase_20 AC 1,259 ms
36,224 KB
testcase_21 AC 1,281 ms
36,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

//------------------------------------------
//------------------------------------------

template<class T>
struct Edge {
    int from, to;
    T cost;

    Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}

    explicit operator int() const { return to; }
};

template<class T>
using Edges = vector<Edge<T>>;

template<class T>
using WeightedGraph = vector<Edges<T>>;

using UnWeightedGraph = vector<vector<int>>;

template<class T>
using DistMatrix = vector<vector<T>>;

struct GraphAdapter {
    template<class T>
    static UnWeightedGraph to_unweighted_graph(const WeightedGraph<T> &origin) {
        int V = origin.size();
        UnWeightedGraph graph(V);
        for (int i = 0; i < V; i++) for (auto &e: origin[i]) graph[i].push_back((int) e);
        return graph;
    }

    static WeightedGraph<int> to_weighted_graph(const UnWeightedGraph &origin) {
        int V = origin.size();
        WeightedGraph<int> graph(V);
        for (int i = 0; i < V; i++) for (auto to: origin[i]) graph[i].push_back({i, to, 1});
        return graph;
    }
};

struct Doubling {
    const long long LOG;
    const int N;
    vector<vector<int>> nexted;

    Doubling(int N, long long log = 60) : N(N), LOG(log) {
        nexted.assign(LOG + 1, vector<int>(N + 1, -1));
    }

    inline void set_init_next(const int v, const int x) {
        nexted[0][v] = x;
    }

    void build() {
        for (int k = 0; k < LOG; k++) {
            for (int v = 0; v < N; v++) {
                if (nexted[k][v] == -1) nexted[k + 1][v] = -1;
                else nexted[k + 1][v] = nexted[k][nexted[k][v]];
            }
        }
    }

    inline int query(int v, long long p) {
        for (long long i = LOG; i >= 0; i--) {
            if (v < 0) return -1;
            if (p & (1LL << i)) {
                v = nexted[i][v];
            }
        }
        return v;
    }
};

template<typename T>
class LowestCommonAncestor {
private:
    const long long LOG;
    const int N;

    const WeightedGraph<T> &graph;
    vector<int> depth;
    vector<T> dist;
    vector<vector<int>> nexted;

public:
    LowestCommonAncestor(const WeightedGraph<T> &graph, const long long LOG = 60) : graph(graph), LOG(LOG), N(graph.size()), depth(N, -1), dist(N, numeric_limits<T>::max()) {
        nexted.assign(LOG + 1, vector<int>(N, -1));
    }

    void dfs(int v, int p, int d, long long sum) {
        depth[v] = d;
        dist[v] = sum;
        nexted[0][v] = p;
        for (auto e: graph[v]) {
            int to = e.to;
            long long cost = e.cost;
            if (to != p) {
                dfs(to, v, d + 1, sum + cost);
            }
        }
    }

    void build(int s) {
        dfs(s, -1, 0, 0);
        for (int k = 0; k < LOG; k++) {
            for (int v = 0; v < N; v++) {
                if (nexted[k][v] == -1) nexted[k + 1][v] = -1;
                else nexted[k + 1][v] = nexted[k][nexted[k][v]];
            }
        }
    }

    int lca(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (long long i = LOG - 1; i >= 0; i--) {
            if ((depth[v] - depth[u]) & (1LL << i)) {
                v = nexted[i][v];
            }
        }
        if (u == v)return u;
        for (long long i = LOG - 1; i >= 0; i--) {
            if (nexted[i][u] != nexted[i][v]) {
                u = nexted[i][u];
                v = nexted[i][v];
            }
        }
        return nexted[0][u];
    }

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

};

int main() {

    int N;
    cin >> N;

    WeightedGraph<long long> graph(N);
    for (int i = 0; i < N - 1; i++) {
        int s, t, w;
        cin >> s >> t >> w;
        graph[s].push_back({s, t, w});
        graph[t].push_back({t, s, w});
    }

    LowestCommonAncestor<long long> LCA(graph);
    LCA.build(0);

    int Q;
    cin >> Q;

    while (Q--) {
        vector<int> xyz(3);
        for (int i = 0; i < 3; i++) cin >> xyz[i];
        sort(xyz.begin(), xyz.end());
        long long ans = LONG_LONG_MAX;
        do {
            long long sum = LCA.get_distance(xyz[0], xyz[1]);
            auto s = LCA.lca(xyz[0], xyz[1]);
            sum += LCA.get_distance(s, xyz[2]);
            ans = min(ans, sum);
        } while (next_permutation(xyz.begin(), xyz.end()));
        cout << ans << endl;
    }

    return 0;
}






























0