結果

問題 No.901 K-ary εxtrεεmε
ユーザー firiexpfiriexp
提出日時 2019-10-04 23:34:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 3,000 ms
コード長 3,705 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 117,868 KB
実行使用メモリ 50,636 KB
最終ジャッジ日時 2023-08-09 21:18:44
合計ジャッジ時間 9,604 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
50,636 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 184 ms
49,024 KB
testcase_08 AC 181 ms
49,044 KB
testcase_09 AC 181 ms
48,828 KB
testcase_10 AC 182 ms
48,760 KB
testcase_11 AC 178 ms
48,756 KB
testcase_12 AC 188 ms
48,804 KB
testcase_13 AC 183 ms
48,724 KB
testcase_14 AC 177 ms
48,792 KB
testcase_15 AC 177 ms
48,792 KB
testcase_16 AC 174 ms
48,832 KB
testcase_17 AC 196 ms
48,768 KB
testcase_18 AC 201 ms
48,832 KB
testcase_19 AC 202 ms
48,792 KB
testcase_20 AC 201 ms
48,780 KB
testcase_21 AC 204 ms
48,820 KB
testcase_22 AC 187 ms
48,788 KB
testcase_23 AC 183 ms
48,832 KB
testcase_24 AC 183 ms
48,728 KB
testcase_25 AC 184 ms
48,960 KB
testcase_26 AC 189 ms
48,656 KB
testcase_27 AC 192 ms
48,824 KB
testcase_28 AC 195 ms
48,732 KB
testcase_29 AC 188 ms
48,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;


template <class F>
struct SparseTable {
    using T = typename F::T;
    vector<vector<T>> table;
    vector<int> u;
    SparseTable() = default;
    explicit SparseTable(const vector<T> &v){ build(v); }

    void build(const vector<T> &v){
        int n = v.size(), m = 1;
        while((1<<m) <= n) m++;
        table.assign(m, vector<T>(n));
        u.assign(n+1, 0);
        for (int i = 2; i <= n; ++i) {
            u[i] = u[i>>1] + 1;
        }
        for (int i = 0; i < n; ++i) {
            table[0][i] = v[i];
        }
        for (int i = 1; i < m; ++i) {
            int x = (1<<(i-1));
            for (int j = 0; j < n; ++j) {
                table[i][j] = F::f(table[i-1][j], table[i-1][min(j+x, n-1)]);
            }
        }
    }

    T query(int a, int b){
        int l = b-a;
        return F::f(table[u[l]][a], table[u[l]][b-(1<<u[l])]);
    }
};


struct F {
    using T = pair<int, int>;
    static T f(T a, T b) { return min(a, b); }
    static T e() { return T{INF<int>, -1}; }
};

class Graph {
    SparseTable<F> table;
    void dfs_euler(int v, int p, int d, int &k){
        id[v] = k;
        vs[k] = v;
        depth[k++] = d;
        for (auto &&u : G[v]) {
            if(u != p){
                dfs_euler(u, v, d+1, k);
                vs[k] = v;
                depth[k++] = d;
            }
        }
    }
public:
    int n;
    vector<vector<int>> G;
    vector<int> vs, depth, id;
    explicit Graph(int n) : n(n), G(n), vs(2*n-1), depth(2*n-1), id(n), table() {};
    void add_edge(int a, int b){
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    void eulertour(int root) {
        int k = 0;
        dfs_euler(root, -1, 0, k);
    }

    void buildLCA(){
        eulertour(0);
        vector<pair<int, int>> v(2*n-1);
        for (int i = 0; i < 2*n-1; ++i) {
            v[i] = make_pair(depth[i], i);
        }
        table.build(v);
    }

    int LCA(int u, int v){
        if(id[u] > id[v]) swap(u, v);
        return table.query(id[u], id[v]+1).second;
    }
};

int main() {
    int n;
    cin >> n;
    using P = pair<int, int>;
    vector<vector<P>> G(n);
    Graph LCA(n);
    for (int i = 0; i < n-1; ++i) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
        LCA.add_edge(u, v);
    }
    vector<ll> dp(n);
    auto dfs = [&](int i, int par, auto &&f) -> void {
        for (auto &&j : G[i]) {
            if(j.first == par) continue;
            dp[j.first] = dp[i] + j.second;
            f(j.first, i, f);
        }
    };
    dfs(0, -1, dfs);
    LCA.buildLCA();
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int k; scanf("%d", &k);
        if(k == 1){
            vector<int> v(k);
            for (auto &&j : v) scanf("%d", &j);
            printf("%lld\n", 0LL);
        }else {
            vector<int> v(k);
            for (auto &&j : v) scanf("%d", &j);
            sort(v.begin(),v.end(), [&](int i, int j){
                return LCA.id[i] < LCA.id[j];
            });
            v.emplace_back(v[0]);
            ll ans = 0;
            for (int j = 0; j < k; ++j) {
                ans += dp[v[j]] + dp[v[j+1]] - dp[LCA.vs[LCA.LCA(v[j], v[j+1])]]*2;
            }
            printf("%lld\n", ans/2);
        }
    }
    return 0;
}
0