卡初始化了求条

P1197 [JSOI2008] 星球大战

dendky @ 2025-01-10 19:33:01

#include <bits/stdc++.h>
using namespace std;
int x, y, k, cnt;
int f[400005], ans[400005];
vector <int>ve[400005];
int find (int a){
    if (f[a]==a) return a;
    else return f[a]=find(f[a]);
}
void join (int a, int b){
    int x1=find(a), x2=find(b);
    if (x1==x2) return;
    f[x1]=x1;
    cnt--;
}
int a[400005];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m;
    cin>>n>>m;
    for (int i=1; i<=n; i++) {
        f[i]=cnt;
        cnt++;//感觉就是这里初始化有问题,但是自己调不出来
    }
    for (int i=1; i<=m; i++) {
        cin>>x>>y;
        ve[y].push_back(x);
        ve[x].push_back(y);
    }
    cin>>k;
    ans[k+1]=cnt;
    for (int i=1; i<=k; i++){
        cin>>a[i];
        f[a[i]]=0;
        cnt--;
    }
    for (int i=0; i<n; i++){
        for (int j=0; j<ve[a[i]].size(); j++){
            int v=ve[a[i]][j];
            if (f[a[i] && f[j]]) join (a[i], v);
        }
        ans[i]=cnt;
    }
    for (int i=1; i<=k+1; i++){
        cout<<ans[i]<<"\n";
    }
    return 0;
}

|