結果

問題 No.3222 Let the World Forget Me
ユーザー GOTKAKO
提出日時 2025-08-01 22:27:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 2,190 ms
コンパイル使用メモリ 214,324 KB
実行使用メモリ 19,004 KB
最終ジャッジ日時 2025-08-01 22:27:49
合計ジャッジ時間 5,913 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<int> P(N);
    for(auto &p : P) cin >> p;  

    vector<set<int>> Graph(N);
    for(int i=0; i<N-1; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).insert(v);
        Graph.at(v).insert(u);
    }

    vector<bool> NG(N);
    vector<int> check(M);
    for(auto &c : check) cin >> c,c--,NG.at(c) = true;
    
    priority_queue<pair<long long,int>> Q;
    for(int i=0; i<N; i++) if(Graph.at(i).size() == 1) Q.push({P.at(i),i});
    long long answer = 0;
    while(true){
        bool ok = false;
        while(Q.size()){
            auto [p,pos] = Q.top(); Q.pop();
            if(NG.at(pos)) continue;
            answer += p;
            int to = *Graph.at(pos).begin();
            Graph.at(to).erase(pos);
            if(Graph.at(to).size() == 1) Q.push({P.at(to),to});
            ok = true; break;
        }
        if(!ok) break;
        vector<int> next;
        for(auto pos : check) for(auto to : Graph.at(pos)) if(!NG.at(to)) NG.at(to) = true,next.push_back(to);
        swap(check,next);
    }

    cout << answer << endl;
}
0