WA10分求调,玄关

P1197 [JSOI2008] 星球大战

tankewei911 @ 2024-10-27 20:48:23

#include<bits/stdc++.h>

using namespace std;

const int MAXX = 4e5 + 5;

int n, m, fa[MAXX], v[MAXX], k, ans[MAXX], a[MAXX], u, vv, cnt, sum, num, id[MAXX], mp[MAXX], vis[MAXX], c[MAXX];
vector<int> e[MAXX];

int find(int x){
//  cout << x << '\n'; 
    if(fa[x] == x){
        return fa[x];
    }
    return fa[x] = find(fa[x]);
}

int main(){
    cin >> n >> m;
    for(int i = 0; i < n; i++) fa[i] = i;
    for(int i = 1; i <= m; i++){
        cin >> u >> vv;
        e[u].push_back(vv);
        e[vv].push_back(u);
    }
    cin >> k;
    for(int i = 1; i <= k; i++){
        cin >> a[i];
        v[a[i]] = 1;
    }
    for(int i = 0; i < n; i++){
        if(!v[i]){
            for(int j = 0; j < e[i].size(); j++){
//              cout << i << " " << e[i][j] << '\n';
                if(!v[e[i][j]]){
                    int x = find(i), y = find(e[i][j]);
                    if(x != y){
                        fa[x] = fa[y];
                    }
                }
            }
        }
    }
//  cout << 114514 << '\n';
    for(int i = 0; i < n; i++){
        int x = find(i);
//      cout << 114514 << '\n'; 
        if(mp[x] == 0 && v[i] == 0){
            ++cnt;
            mp[x] = 1;
            id[x] = cnt;
        }
    }
    int anss = cnt;
    for(int i = k; i >= 1; i--){
        ans[i] = cnt;
        num = 0;
        sum++;
        for(int j = 0; j < e[a[i]].size(); j++){
            if(!v[e[a[i]][j]]){
                int x = find(e[a[i]][j]);
                if(vis[x] != sum){
                    fa[a[i]] = fa[x];
                    c[++num] = x;
                    vis[x] = sum;
                }
            }
        }
        for(int j = 2; j <= num; j++){
            ans[i]--;
            fa[c[j]] = fa[c[1]];
            cnt--;
        }
        v[a[i]] = 0;
    }
    for(int i = 1; i <= k; i++){
        cout << ans[i] << '\n';
    }
    cout << anss << '\n';
    return 0;
} 

|