結果
問題 |
No.3222 Let the World Forget Me
|
ユーザー |
![]() |
提出日時 | 2025-08-01 22:52:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 204 ms / 2,000 ms |
コード長 | 2,348 bytes |
コンパイル時間 | 4,412 ms |
コンパイル使用メモリ | 265,000 KB |
実行使用メモリ | 24,492 KB |
最終ジャッジ日時 | 2025-08-01 22:52:16 |
合計ジャッジ時間 | 9,432 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 |
ソースコード
#ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #include <cassert> #include <atcoder/all> using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; //using mint = modint1000000007; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repu(i, s, t) for (int i = (int)(s); i < (int)(t); i++) #define repd(i, s, t) for (int i = (int)(s)-1; i >= (int)(t); i--) #define all(v) v.begin(), v.end() void _u() { cerr << endl; } template <class H, class... T> void _u(H&& h, T&&... t) { cerr << h << ", "; _u(move(t)...); } #define U(...) { cerr << #__VA_ARGS__ << ": "; _u(__VA_ARGS__); } template<typename T> bool chmax(T &a, const T b) { if(a >= b) return false; a = b; return true; } template<typename T> bool chmin(T &a, const T b) { if(a <= b) return false; a = b; return true; } template<typename T> istream& operator>>(istream &in, vector<T> &a) { for(T &x: a) in >> x; return in; } template<typename T> ostream& operator<<(ostream &out, const vector<T> &a) { for(const T &x: a) out << x << ' '; return out; } const int di[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dj[] = {0, 1, 0, -1, -1, 1, 1, -1, 0}; int main() { int n, m; cin >> n >> m; vector<int> p(n); cin >> p; vector<vector<int>> g(n); rep(i, n-1) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } vector<int> c(m); cin >> c; rep(i, m) c[i]--; const int INF = 1e9; vector<int> dist(n, INF); rep(i, m) dist[c[i]] = 0; queue<int> wait; rep(i, m) wait.push(c[i]); while(!wait.empty()) { int pos = wait.front(); wait.pop(); for(int to: g[pos]) { if(dist[to] < INF) continue; dist[to] = dist[pos] + 1; wait.push(to); } } vector<set<int>> g2(n); rep(i, n) g2[i] = set<int>(all(g[i])); priority_queue<pair<int, int>> pq; ll ans = 0; rep(i, n) if(g2[i].size() <= 1) pq.emplace(p[i], i); int b = 1; while(!pq.empty()) { auto[pi, i] = pq.top(); pq.pop(); if(dist[i] < b) continue; ans += pi; b++; int par = *g2[i].begin(); g2[par].erase(i); if(g2[par].size() <= 1) pq.emplace(p[par], par); } cout << ans << endl; return 0; }