結果

問題 No.901 K-ary εxtrεεmε
ユーザー tomatoma
提出日時 2019-10-04 22:12:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 349 ms / 3,000 ms
コード長 3,652 bytes
コンパイル時間 2,207 ms
コンパイル使用メモリ 189,336 KB
実行使用メモリ 28,928 KB
最終ジャッジ日時 2024-04-15 00:08:35
合計ジャッジ時間 9,655 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
28,928 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 242 ms
24,676 KB
testcase_08 AC 222 ms
24,832 KB
testcase_09 AC 231 ms
24,704 KB
testcase_10 AC 222 ms
24,832 KB
testcase_11 AC 244 ms
24,668 KB
testcase_12 AC 225 ms
24,632 KB
testcase_13 AC 213 ms
24,816 KB
testcase_14 AC 212 ms
24,832 KB
testcase_15 AC 220 ms
24,704 KB
testcase_16 AC 211 ms
24,792 KB
testcase_17 AC 262 ms
24,656 KB
testcase_18 AC 270 ms
24,680 KB
testcase_19 AC 283 ms
24,704 KB
testcase_20 AC 276 ms
24,664 KB
testcase_21 AC 289 ms
24,632 KB
testcase_22 AC 215 ms
24,960 KB
testcase_23 AC 203 ms
24,704 KB
testcase_24 AC 216 ms
24,800 KB
testcase_25 AC 224 ms
24,704 KB
testcase_26 AC 224 ms
24,832 KB
testcase_27 AC 331 ms
24,704 KB
testcase_28 AC 332 ms
24,832 KB
testcase_29 AC 349 ms
24,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using WGraph = vector<vector<pair<int, int>>>;

using Graph = vector<vector<int>>;
struct HLDecomposition {
    using pii = pair<int, int>;
    int n;
    Graph G;
    vector<int> vid, inv, par, depth, subsize, head, prev, next, type;

    HLDecomposition(const Graph& G_) :
        n(G_.size()), G(G_),
        vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1),
        head(n), prev(n, -1), next(n, -1), type(n) {}
    void build(vector<int> roots = { 0 }) {
        int curtype = 0, pos = 0;
        for (int root : roots) {
            decide_heavy_edge(root);
            reconstruct(root, curtype++, pos);
        }
    }
    void decide_heavy_edge(int root) {
        stack<pii> st;
        par[root] = -1, depth[root] = 0;
        st.emplace(root, 0);
        while (!st.empty()) {
            int now = st.top().first;
            int& way = st.top().second;
            if (way < G[now].size()) {
                int child = G[now][way++];
                if (child == par[now])continue;
                par[child] = now;
                depth[child] = depth[now] + 1;
                st.emplace(child, 0);
            }
            else {
                st.pop();
                int maxsize = 0;
                for (auto child : G[now]) {
                    if (child == par[now])continue;
                    subsize[now] += subsize[child];
                    if (maxsize < subsize[child]) {
                        maxsize = subsize[child];
                        prev[child] = now;
                        next[now] = child;
                    }
                }
            }
        }
    }
    void reconstruct(int root, int curtype, int& pos) {
        stack<int> st({ root });
        while (!st.empty()) {
            int start = st.top(); st.pop();
            for (int v = start; v != -1; v = next[v]) {
                type[v] = curtype;
                vid[v] = pos++;
                inv[vid[v]] = v;
                head[v] = start;
                for (auto child : G[v]) {
                    if (child != par[v] && child != next[v]) {
                        st.push(child);
                    }
                }
            }
        }
    }
    int lca(int u, int v) {
        while (true) {
            if (vid[u] > vid[v])swap(u, v);
            if (head[u] == head[v])return u;
            v = par[head[v]];
        }
    }
};


void dfs(const WGraph& g, vector<ll>& d, int now, int par) {
    for (auto next : g[now]) {
        int v, w;
        tie(v, w) = next;
        if (v == par)continue;

        d[v] = d[now] + w;
        dfs(g, d, v, now);
    }
}

int main()
{
    int N, Q;
    cin >> N;
    WGraph wg(N);
    Graph g(N);
    rep(i, N - 1) {
        int u, v, w;
        cin >> u >> v >> w;
        wg[u].emplace_back(v, w);
        wg[v].emplace_back(u, w);
        g[u].push_back(v);
        g[v].push_back(u);
    }

    HLDecomposition hld(g); hld.build();
    vector<ll> d(N);
    dfs(wg, d, 0, -1);
    auto dist = [&](int l, int r) {
        return d[l] + d[r] - 2 * d[hld.lca(l, r)];
    };

    cin >> Q;
    while (Q--) {
        ll k;
        cin >> k;
        vector<ll> x(k);
        rep(i, k)cin >> x[i];
        for_each(x.begin(), x.end(), [&](ll& v) {v = hld.vid[v]; });
        sort(x.begin(), x.end());
        for_each(x.begin(), x.end(), [&](ll& v) {v = hld.inv[v]; });

        ll res = 0;
        rep(i, k) {
            res += dist(x[i], x[(i + 1) % k]);
        }
        cout << res / 2 << endl;
    }
    return 0;
}
0