結果

問題 No.901 K-ary εxtrεεmε
ユーザー catuppercatupper
提出日時 2019-10-05 01:44:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,165 bytes
コンパイル時間 997 ms
コンパイル使用メモリ 92,728 KB
実行使用メモリ 48,920 KB
最終ジャッジ日時 2024-04-14 23:44:03
合計ジャッジ時間 10,026 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
    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