結果

問題 No.901 K-ary εxtrεεmε
ユーザー 👑 NachiaNachia
提出日時 2020-09-27 19:44:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 322 ms / 3,000 ms
コード長 1,728 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 174,836 KB
実行使用メモリ 23,384 KB
最終ジャッジ日時 2023-09-13 02:13:46
合計ジャッジ時間 9,943 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
23,384 KB
testcase_01 AC 5 ms
13,404 KB
testcase_02 AC 6 ms
13,464 KB
testcase_03 AC 6 ms
13,460 KB
testcase_04 AC 6 ms
13,484 KB
testcase_05 AC 6 ms
13,488 KB
testcase_06 AC 6 ms
13,440 KB
testcase_07 AC 195 ms
20,572 KB
testcase_08 AC 195 ms
20,656 KB
testcase_09 AC 194 ms
20,576 KB
testcase_10 AC 197 ms
20,572 KB
testcase_11 AC 193 ms
20,580 KB
testcase_12 AC 183 ms
20,572 KB
testcase_13 AC 183 ms
20,568 KB
testcase_14 AC 181 ms
20,552 KB
testcase_15 AC 183 ms
20,664 KB
testcase_16 AC 182 ms
20,728 KB
testcase_17 AC 259 ms
20,596 KB
testcase_18 AC 260 ms
20,576 KB
testcase_19 AC 262 ms
20,620 KB
testcase_20 AC 267 ms
20,576 KB
testcase_21 AC 257 ms
20,604 KB
testcase_22 AC 174 ms
20,700 KB
testcase_23 AC 171 ms
20,668 KB
testcase_24 AC 173 ms
20,788 KB
testcase_25 AC 182 ms
20,668 KB
testcase_26 AC 185 ms
20,672 KB
testcase_27 AC 322 ms
20,528 KB
testcase_28 AC 315 ms
20,576 KB
testcase_29 AC 311 ms
20,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const int xN = 100000;

struct Edge { int to; LL dist; };

int N;
vector<Edge> E[xN];
int P[xN];
LL D[xN];
int dfsI[xN];
int lcaP[20][xN];
int lcaD[xN];

int dfs(int p = 0, int i = 0) {
    dfsI[p] = i++;
    for (Edge& e : E[p]) {
        if (P[p] == e.to) continue;
        D[e.to] = D[p] + e.dist;
        lcaD[e.to] = lcaD[p] + 1;
        P[e.to] = p;
        i = dfs(e.to, i);
    }
    return i;
}

void init() {
    rep(i, N) P[i] = -1;
    rep(i, N) D[i] = 0;
    dfs(0);

    rep(i, N) lcaP[0][i] = P[i];
    lcaP[0][0] = 0;
    rep(d, 19) rep(i, N) lcaP[d + 1][i] = lcaP[d][lcaP[d][i]];
}

int LCA(int u, int v) {
    if (lcaD[u] > lcaD[v]) swap(u, v);
    for (int i = 19; i >= 0; i--) if (lcaD[u] + (1 << i) <= lcaD[v]) v = lcaP[i][v];
    if (u == v) return u;
    for (int i = 19; i >= 0; i--) if (lcaP[i][u] != lcaP[i][v]) {
        u = lcaP[i][u];
        v = lcaP[i][v];
    }
    return lcaP[0][u];
}

LL distance(int u, int v) {
    int g = LCA(u, v);
    return D[u] + D[v] - 2 * D[g];
}

int main() {
    cin >> N;
    rep(i, N - 1) {
        int u, v, c; cin >> u >> v >> c;
        E[u].push_back({ v,c });
        E[v].push_back({ u,c });
    }
    init();

    int Q; cin >> Q;
    while (Q--) {
        int K; cin >> K;
        vector<pair<int,int>> X(K);
        rep(i, K) { int x; cin >> x; X[i] = { dfsI[x],x }; }
        sort(X.begin(), X.end()); // dfsで訪れた順にソート
        X.push_back(X.front());
        LL ans = 0;
        rep(i, K) ans += distance(X[i].second, X[i + 1].second);
        cout << (ans / 2) << endl;
    }

    return 0;
}
0