我真惨,,,80分

P1197 [JSOI2008] 星球大战

blackfrog @ 2020-02-13 23:06:46

rt,错的是第6,第9个数据

#include<iostream>
#include<vector>
using namespace std;
vector<long long> a[1000100];
long long fa[1000100];
long long order[1000100];
bool isdes[1000100];
long long m,n,k;
long long cnt;
long long ans[1000100];
void init(long long len){
    for(long long i = 0;i<len;i++){
        fa[i] = i;
    }
}

long long find(long long x){
    if(fa[x]==x)    return x;
    return fa[x]=find(fa[x]);
}

void merge(long long x,long long y){
    if(find(x)!=find(y)) fa[find(x)] = find(y);
}

bool equal(long long x,long long y){
    return find(x)==find(y);
}

int main(){
    cin>>n>>m;
    cnt = n;
    init(n);
    for(long long i = 0;i<n;i++){
        a[i].push_back(-1);
    }
    for(long long i = 0;i<m;i++){
        long long u,v;
        cin>>u>>v;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    cin>>k;
    for(long long i = 0;i<k;i++){
        cin>>order[i];
        isdes[order[i]] = 1;
    }

    for(long long i = 0;i<n;i++){
        if(!isdes[i]){
            for(long long j = 0;j<a[i].size();j++){
                if(!isdes[a[i][j]]&&!equal(i,a[i][j])&&a[order[i]][j]!=(-1)){
                    merge(i,a[i][j]);
                    cnt--;
                }
            }
        }
    }
    ans[k] = cnt-k;
    for(long long i = k-1;i>=0;i--){
        isdes[order[i]] = 0;
        for(long long j = 0;j<a[order[i]].size();j++){
            if((!isdes[a[order[i]][j]])&&(!equal(order[i],a[order[i]][j]))&&a[order[i]][j]!=(-1)){
                merge(order[i],a[order[i]][j]);
                cnt--;
            }
        }
        ans[i] = cnt-i;
    }
    for(long long i = 0;i<=k;i++){
        cout<<ans[i]<<endl;
    }
    return 0;
}

|