結果

問題 No.901 K-ary εxtrεεmε
ユーザー t98slidert98slider
提出日時 2023-04-30 22:06:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 3,000 ms
コード長 1,889 bytes
コンパイル時間 1,974 ms
コンパイル使用メモリ 182,740 KB
実行使用メモリ 22,152 KB
最終ジャッジ日時 2023-08-12 11:06:41
合計ジャッジ時間 8,846 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
22,152 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 100 ms
17,300 KB
testcase_08 AC 96 ms
17,160 KB
testcase_09 AC 99 ms
17,212 KB
testcase_10 AC 99 ms
17,168 KB
testcase_11 AC 93 ms
17,156 KB
testcase_12 AC 96 ms
17,236 KB
testcase_13 AC 97 ms
17,212 KB
testcase_14 AC 98 ms
17,208 KB
testcase_15 AC 97 ms
17,160 KB
testcase_16 AC 92 ms
17,176 KB
testcase_17 AC 99 ms
17,304 KB
testcase_18 AC 104 ms
17,172 KB
testcase_19 AC 96 ms
17,272 KB
testcase_20 AC 98 ms
17,184 KB
testcase_21 AC 102 ms
17,152 KB
testcase_22 AC 94 ms
17,424 KB
testcase_23 AC 97 ms
17,560 KB
testcase_24 AC 98 ms
17,440 KB
testcase_25 AC 98 ms
17,448 KB
testcase_26 AC 94 ms
17,536 KB
testcase_27 AC 92 ms
17,180 KB
testcase_28 AC 94 ms
17,236 KB
testcase_29 AC 92 ms
17,308 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