結果

問題 No.901 K-ary εxtrεεmε
ユーザー catuppercatupper
提出日時 2019-10-05 01:57:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,202 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 92,740 KB
実行使用メモリ 34,820 KB
最終ジャッジ日時 2024-04-15 00:01:21
合計ジャッジ時間 6,969 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 4 ms
12,208 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 372 ms
30,428 KB
testcase_28 AC 366 ms
30,804 KB
testcase_29 AC 362 ms
30,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

Int ppp;
Int come[110000];
Int p[110000][20];
Int depth[110000];
Int node[110000];
Int weight[110000];
vector<P> edge[110000];

void dfs(Int x, Int last = -1, Int d = 0){
    come[x] = ppp;
    node[ppp] = x;
    depth[x] = d;
    ppp++;
    for(auto p:edge[x]){
        Int to = p.first, w = p.second;
        if(to == last)continue;
        weight[to] = weight[x] + w;
        dfs(to, x, d + 1);
    }
    if(last != -1){
        for(Int i = 0;i < 20;i++){
            if(i == 0)p[x][i] = last;
            else p[x][i] = p[p[x][i-1]][i-1];
        }
    }
}

Int la(Int x, Int d, Int pp = 19){
    if(d == 0)return x;
    while((d & (1 << pp)) == 0)pp--;
    x = p[x][pp];
    return la(x, d - (1 << pp), pp);
}

Int lca(Int x, Int y){
    if(depth[x] < depth[y])swap(x, y);
    if(depth[x] > depth[y])x = la(x, depth[x] - depth[y]);
    if(x == y)return x;
    if(depth[x] != depth[y])exit(1);
    Int up = 19;
    while(p[x][0] != p[y][0]){
        while(p[x][up] == p[y][up])up--;
        x = p[x][up];
        y = p[y][up];
    }
    return p[x][0];
}

void solve(){
    Int k, ans = 0;
    cin >> k;
    vector<Int> nodes;
    for(Int i = 0;i < k;i++){
        Int x;
        cin >> x;
        nodes.push_back(come[x]);
    }
    sort(nodes.begin(), nodes.end());
    for(Int i = 0;i < k;i++){
        Int x = node[nodes[i]];
        Int y = node[nodes[(i+1)%k]];
        Int l = lca(x, y);
        //cout << x << ":" << weight[x] << " "  << y << ":" << weight[y] << " "  << l << ":" << weight[l] << endl; 
        ans += weight[x] - weight[l];
        ans += weight[y] - weight[l];
    }
    cout << ans / 2 << endl;
}

int main(){
    int n, u, v, w, q;
    cin >> n;
    for(Int i = 0;i < n-1;i++){
        cin >> u >> v >> w;
        edge[u].push_back(P(v,w));
        edge[v].push_back(P(u,w));
    }
    dfs(0);
    cin >> q;
    while(q--)solve();
    return 0;
}
0