結果
問題 |
No.3222 Let the World Forget Me
|
ユーザー |
![]() |
提出日時 | 2025-08-02 22:38:48 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 126 ms / 2,000 ms |
コード長 | 1,959 bytes |
コンパイル時間 | 3,264 ms |
コンパイル使用メモリ | 298,304 KB |
実行使用メモリ | 20,112 KB |
最終ジャッジ日時 | 2025-08-02 22:38:55 |
合計ジャッジ時間 | 6,795 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 |
ソースコード
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n) - 1; i >= 0; --i) #define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair<int,int> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; cin >> n >> m; vector<int>p(n); rep(i, n)cin >> p[i]; vector<set<int>>g(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; g[a].insert(b); g[b].insert(a); } vector<int>c(m); rep(i, m)cin >> c[i], c[i]--; vector<int>dp(n, INF); queue<pair<int, int>> q; rep(i, m) { q.emplace(c[i], 0); dp[c[i]] = 0; } while (!q.empty()) { auto[v, d] = q.front(); q.pop(); for (int nv : g[v]) { if (chmin(dp[nv], dp[v] + 1))q.emplace(nv, dp[nv]); } } vector<int> deg(n); rep(i, n)deg[i] = g[i].size(); priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>> pq; rep(i, n) { if (deg[i] == 1) pq.emplace(p[i], dp[i], i);// 価値,時刻,index } long long ans = 0; int now = 0; while (!pq.empty()) { while (!pq.empty()) { auto[_p, _d, _i] = pq.top(); pq.pop(); if (_d <= now)continue; ans += _p; int par = *g[_i].begin(); g[par].erase(_i); deg[par]--; if (deg[par] == 1) { pq.emplace(p[par], dp[par], par); } break; } now++; } cout << ans << endl; return 0; }