結果

問題 No.3222 Let the World Forget Me
ユーザー umimel
提出日時 2025-08-01 22:54:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,790 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 181,060 KB
実行使用メモリ 22,880 KB
最終ジャッジ日時 2025-08-01 22:54:12
合計ジャッジ時間 7,226 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:43:14: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   43 |         auto [v, i] = *st.rbegin();
      |              ^

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


void solve(){
    int n, m; cin >> n >> m;
    vector<int> p(n); for(int i=0; i<n; i++) cin >> p[i];
    vector<set<int>> T(n);
    for(int i=0; i<n-1; i++){
        int u, v; cin >> u >> v;
        u--; v--;
        T[u].insert(v);
        T[v].insert(u);
    }

    vector<bool> used(n, false);
    set<int> que;
    set<pair<ll, int>> st; for(int i=0; i<n; i++) if((int)T[i].size() == 1) st.insert({p[i], i});
    for(int i=0; i<m; i++){
        int c; cin >> c;
        c--;
        used[c] = true;
        que.insert(c);
        st.erase({p[c], c});
    }

    ll ans = 0LL;
    for(int t=1; ; t++){
        if(st.empty()) break;
        auto [v, i] = *st.rbegin();
        ans += v;
        used[i] = true;
        que.erase(i);
        st.erase({v, i});
        int to = *T[i].begin();
        if(!used[to]){
            T[to].erase(i);
            if((int)T[to].size() == 1) st.insert({p[to], to});
        }

        set<int> nxt;
        for(auto v : que){
            for(int u : T[v]){
                if(used[u]) continue;
                used[u] = true;
                nxt.insert(u);
                st.erase({p[u], u});
            }
        }
        que = nxt;
    }

    cout << ans << endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0