結果

問題 No.901 K-ary εxtrεεmε
ユーザー t98slidert98slider
提出日時 2023-04-30 22:06:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 3,000 ms
コード長 1,889 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 183,320 KB
実行使用メモリ 22,380 KB
最終ジャッジ日時 2024-04-30 01:10:27
合計ジャッジ時間 7,224 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
22,380 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 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 99 ms
17,576 KB
testcase_08 AC 104 ms
17,576 KB
testcase_09 AC 102 ms
17,444 KB
testcase_10 AC 102 ms
17,572 KB
testcase_11 AC 102 ms
17,704 KB
testcase_12 AC 102 ms
17,580 KB
testcase_13 AC 102 ms
17,576 KB
testcase_14 AC 101 ms
17,580 KB
testcase_15 AC 102 ms
17,576 KB
testcase_16 AC 102 ms
17,580 KB
testcase_17 AC 111 ms
17,452 KB
testcase_18 AC 102 ms
17,576 KB
testcase_19 AC 101 ms
17,568 KB
testcase_20 AC 102 ms
17,576 KB
testcase_21 AC 102 ms
17,576 KB
testcase_22 AC 104 ms
17,468 KB
testcase_23 AC 100 ms
17,576 KB
testcase_24 AC 107 ms
17,576 KB
testcase_25 AC 104 ms
17,576 KB
testcase_26 AC 103 ms
17,704 KB
testcase_27 AC 97 ms
17,580 KB
testcase_28 AC 100 ms
17,576 KB
testcase_29 AC 95 ms
17,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, u, v, w, Q;
    cin >> n;
    vector<vector<pair<int, int>>> g(n);
    for(int i = 1; i < n; i++){
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    int logv = __lg(n), timer = 0;
    vector<vector<int>> parent(logv + 1, vector<int>(n, -1));
    vector<int> depth(n), order(n);
    vector<ll> dist(n);
    function<void(int, int)> dfs = [&](int v, int p){
        order[v] = timer++;
        int log_v = __lg(max(1, depth[v]));
        for(int i = 0; i < log_v; i++){
            parent[i + 1][v] = parent[i][v] == -1 ? -1 : parent[i][parent[i][v]];
        }
        for(auto &&pa : g[v]){
            tie(u, w) = pa;
            if(u == p) continue;
            parent[0][u] = v;
            depth[u] = depth[v] + 1;
            dist[u] = dist[v] + w;
            dfs(u, v);
        }
    };
    dfs(0, -1);
    auto lca = [&](int u, int v){
        if(depth[u] > depth[v]) swap(u, v);
        int d = depth[v] - depth[u];
        while(d){
            v = parent[__lg(d & -d)][v];
            d -= d & -d;
        }
        if(u == v) return v;
        for(int i = __lg(depth[v]); i >= 0; i--){
            if(parent[i][v] != parent[i][u]){
                v = parent[i][v];
                u = parent[i][u];
            }
        }
        return parent[0][v];
    };

    cin >> Q;
    while(Q--){
        int k;
        cin >> k;
        vector<int> ver(k);
        for(auto &&v : ver) cin >> v;
        sort(ver.begin(), ver.end(), [&](int vl, int vr){return order[vl] < order[vr];});
        ll ans = 0;
        for(int i = 0; i < k; i++){
            ans += dist[ver[i]];
            ans -= dist[lca(ver[i], ver[i + 1 == k ? 0 : i + 1])];
        }
        cout << ans << '\n';
    }
}
0