結果

問題 No.3222 Let the World Forget Me
ユーザー 沙耶花
提出日時 2025-08-01 22:04:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 1,333 bytes
コンパイル時間 4,576 ms
コンパイル使用メモリ 269,312 KB
実行使用メモリ 11,352 KB
最終ジャッジ日時 2025-08-01 22:04:46
合計ジャッジ時間 9,147 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000005
#define Inf64 1000000000000000001LL

int main(){
	int N,M;
	cin>>N>>M;
	
	vector<long long> p(N);
	rep(i,N)cin>>p[i];
	vector<vector<int>> E(N);
	rep(i,N-1){
		int a,b;
		cin>>a>>b;
		a--; b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	vector<bool> ng(N,false);
	queue<int> Q;
	rep(i,M){
		int t;
		cin>>t;
		ng[t-1] = true;
		Q.push(t-1);
	}
	vector<bool> cut(N,false);
	long long ans = 0;
	priority_queue<pair<long long,int>> leaves;
	vector<int> ds(N);
	rep(i,N)ds[i] = E[i].size();
	rep(i,N){
		if(ds[i]==1)leaves.push({p[i], i});
	}
	while(true){
		int ii = -1;
		while(leaves.size()>0){
			auto t = leaves.top();
			leaves.pop();
			if(!ng[t.second]){
				ii = t.second;
				break;
			}
		}
		if(ii==-1)break;
		ans += p[ii];
		cut[ii] = true;
		rep(i,E[ii].size()){
			int to = E[ii][i];
			ds[to]--;
			if(ds[to]==1){
				leaves.push({p[to], to});
			}
		}
		queue<int> nQ;
		while(Q.size()>0){
			int u = Q.front();
			Q.pop();
			rep(i,E[u].size()){
				int v = E[u][i];
				if(cut[v])continue;
				if(ng[v])continue;
				ng[v] = true;
				nQ.push(v);
			}
		}
		Q = nQ;
	}
	cout<<ans<<endl;
	return 0;
}
0