結果

問題 No.901 K-ary εxtrεεmε
ユーザー parukiparuki
提出日時 2019-10-10 20:53:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 3,000 ms
コード長 4,733 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 196,936 KB
実行使用メモリ 14,900 KB
最終ジャッジ日時 2023-08-14 03:53:43
合計ジャッジ時間 8,446 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
12,892 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 158 ms
12,524 KB
testcase_08 AC 163 ms
12,524 KB
testcase_09 AC 160 ms
12,684 KB
testcase_10 AC 159 ms
12,528 KB
testcase_11 AC 164 ms
12,620 KB
testcase_12 AC 160 ms
12,648 KB
testcase_13 AC 162 ms
12,784 KB
testcase_14 AC 167 ms
12,748 KB
testcase_15 AC 163 ms
12,544 KB
testcase_16 AC 157 ms
12,656 KB
testcase_17 AC 179 ms
12,672 KB
testcase_18 AC 188 ms
12,672 KB
testcase_19 AC 187 ms
12,800 KB
testcase_20 AC 181 ms
12,624 KB
testcase_21 AC 182 ms
12,640 KB
testcase_22 AC 174 ms
14,500 KB
testcase_23 AC 179 ms
14,440 KB
testcase_24 AC 169 ms
14,624 KB
testcase_25 AC 176 ms
14,264 KB
testcase_26 AC 176 ms
14,900 KB
testcase_27 AC 217 ms
12,652 KB
testcase_28 AC 222 ms
12,672 KB
testcase_29 AC 214 ms
12,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

class HLDecomposition {
    int cur = 0;
    void bfs(const vector<vector<int> > &G) {
        queue<tuple<int, int, int> > que;
        vector<int> order;
        que.push(make_tuple(0, -1, 0));
        while (!que.empty()) {
            int u, pre, d;
            tie(u, pre, d) = que.front();
            que.pop();
            parent[u] = pre;
            depth[u] = d;
            order.push_back(u);
            for (int v : G[u])if (v != pre) {
                que.push(make_tuple(v, u, d + 1));
            }
        }
        reverse(order.begin(), order.end());
        for (int u : order) {
            sub[u] = 1;
            for (int v : G[u])if (v != parent[u])sub[u] += sub[v];
        }
    }

    void dfs_stk(const vector<vector<int> > & G) {
        stack<pair<int, int> > stk;
        stk.push(make_pair(0, 0));
        while (!stk.empty()) {
            int u, h, pre;
            tie(u, h) = stk.top();
            stk.pop();
            head[u] = h;
            id[u] = cur++;
            pre = parent[u];
            int heavy = -1, maxi = 0;
            for (int v : G[u]) {
                if (v != pre && maxi < sub[v]) {
                    maxi = sub[v];
                    heavy = v;
                }
            }
            for (int v : G[u]) {
                if (v != pre && v != heavy) {
                    stk.push(make_pair(v, v));
                }
            }
            if (heavy != -1)stk.push(make_pair(heavy, h));
        }
    }
public:
    vector<int> parent, depth, sub, id, head;

    HLDecomposition(const vector<vector<int> > &G) {
        parent = depth = sub = id = head = vector<int>(G.size());
        bfs(G);
        dfs_stk(G);
    }

    /*
    パス(u, v)で通る頂点を半開区間の集合に変換する。
    O(log(|V|))
    */
    vector<pair<int, int> > goUpToLCA(int u, int v) {
        vector<pair<int, int> > res;
        while (true) {
            if (head[u] == head[v]) {
                if (depth[u] > depth[v])swap(u, v);
                res.emplace_back(id[u], id[v] + 1);
                break;
            } else {
                if (depth[head[u]] > depth[head[v]])swap(u, v);
                res.emplace_back(id[head[v]], id[v] + 1);
                v = parent[head[v]];
            }
        }
        reverse(res.begin(), res.end());
        return res;
    }

    int getLCA(int u, int v) {
        while (true) {
            if (head[u] == head[v]) {
                if (depth[u] > depth[v])swap(u, v);
                break;
            } else {
                if (depth[head[u]] > depth[head[v]])swap(u, v);
                v = parent[head[v]];
            }
        }
        return u;
    }
};

void solve() {
    int N;
    cin >> N;
    vi U(N-1), V(N-1), W(N-1);
    vector<vi> G(N);
    rep(i, N - 1) {
        cin >> U[i] >> V[i] >> W[i];
        G[U[i]].push_back(V[i]);
        G[V[i]].push_back(U[i]);
    }

    HLDecomposition hld(G);
    vll sm(N + 1);
    rep(i, N - 1) {
        if (hld.depth[V[i]] < hld.depth[U[i]]) {
            swap(U[i], V[i]);
        }
        sm[hld.id[V[i]] + 1] = W[i];
    }
    rep(i, N)sm[i + 1] += sm[i];

    int Q;
    cin >> Q;
    vector<pii> S;
    rep(ITER, Q) {
        S.clear();
        int K;
        cin >> K;
        vi X(K);
        rep(i, K) {
            cin >> X[i];
        }
        int lca = X[0];
        FOR(i, 1, K) {
            auto ps = hld.goUpToLCA(X[0], X[i]);
            lca = hld.getLCA(lca, X[i]);
            S.insert(S.end(), all(ps));
        }
        sort(all(S));
        S.emplace_back(N + 1, N + 1);

        ll ans = 0;
        int l = -1, r = -1;
        rep(i, sz(S)) {
            if (S[i].first <= r) {
                smax(r, S[i].second);
            } else {
                if (i > 0) {
                    ans += sm[r] - sm[l];
                }
                tie(l, r) = S[i];
            }
        }
        if (sz(S) > 1) {
            int id = hld.id[lca];
            ans -= sm[id + 1] - sm[id];
        }
        cout << ans << endl;
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0