結果

問題 No.3222 Let the World Forget Me
ユーザー kenti
提出日時 2025-08-01 23:10:19
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 1,350 bytes
コンパイル時間 2,874 ms
コンパイル使用メモリ 291,808 KB
実行使用メモリ 11,712 KB
最終ジャッジ日時 2025-08-01 23:10:26
合計ジャッジ時間 5,292 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int n, m; cin >> n >> m;
    vector<ll> p(n);
    for (ll &x : p) cin >> x;
    vector<vector<int>> edges(n);
    vector<int> deg(n, 0);
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
        deg[a]++; deg[b]++;
    }
    vector<int> c(m), ct(n, -1);
    for (int &x : c) cin >> x;
    queue<int> que;
    for (int x : c) {
        que.push(x - 1);
        ct[x - 1] = 0;
    }
    while (!que.empty()) {
        int v = que.front(); que.pop();
        for (int u : edges[v]) {
            if (ct[u] >= 0) continue;
            que.push(u);
            ct[u] = ct[v] + 1;
        }
    }
    priority_queue<pair<ll, int>> pq;
    for (int i = 0; i < n; i++) {
        if (deg[i] == 1) pq.push({p[i], i});
    }
    int t = 0; ll ans = 0;
    while (!pq.empty()) {
        auto [val, v] = pq.top(); pq.pop();
        if (ct[v] > t) {
            ans += val;
            t++;
            for (int u : edges[v]) {
                deg[u]--;
                if (deg[u] == 1) {
                    pq.push({p[u], u});
                }
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
0