0分悬关求调

P1197 [JSOI2008] 星球大战

chenyyegg @ 2024-06-14 11:57:15

#include<bits/stdc++.h>
using namespace std;
int n,m,k,f[400005],n2;
vector<int>v[400005],vec;
int find(int x){
    return f[x]==x?x:f[x]=find(f[x]);
}
bool merge(int x,int y){
    int fx=find(x),fy=find(y);
    return fx==fy?false:(f[fx]=fy,n2--,true);
}
int main(){
    cin>>n>>m;n2=n;
    for(int i=1;i<=n;i++)f[i]=i;
    for(int i=1,x,y;i<=m;i++)
        {
            cin>>x>>y;
            v[x].push_back(y);v[y].push_back(x);
        }
    cin>>k;
    int a[k+5];bool b[n+5]={false};
    for(int i=1;i<=k;i++)cin>>a[i];
    for(int i=k;i>=1;i--)b[a[i]]=true;
    for(int i=1;i<=n;i++)if(b[i])for(int j=0;j<v[i].size();j++)merge(i,v[i][j]);
    cout<<n2<<endl;
    for(int i=1;i<=k;i++)
        {
            for(int j=0;j<v[a[i]].size();i++)merge(a[i],v[a[i]][j]);
            vec.push_back(n2);
        }
    for(int i=vec.size()-1;i>=0;i--)cout<<vec[i]<<endl;
    return 0;
}

3RE+7WA


by 123huchenghao @ 2024-06-28 14:27:23

#include<cstdio>
int n,m,t,sum,cnt,tot;
int st[400010],fa[400010],head[400010],lian[400010],ans[400010];
bool flag[400010];
struct edg{
    int to,next,from;
}edge[400010];

void add(int x,int y){
    edge[++cnt].to=y;
    edge[cnt].from=x;
    edge[cnt].next=head[x];
    head[x]=cnt;
}

int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        add(a,b);
        add(b,a);
    }
    scanf("%d",&t);
    for(register int i=1;i<=t;i++) {
        scanf("%d",&st[i]);
        flag[st[i]]=true;
    }
    for(register int i=0;i<n;i++) fa[i]=i;
    int tot=n-t;
    for(int i=1;i<=cnt;i++){
        int u=find(edge[i].to),v=find(edge[i].from);
        if(u!=v&&!flag[edge[i].to]&&!flag[edge[i].from]) tot--,fa[u]=v;
    }
    ans[t+1]=tot;
    for(int i=t;i;i--){
        int x=st[i];
        tot++,flag[x]=false;
        for(int j=head[x];j;j=edge[j].next){
            int u=find(x),v=find(edge[j].to);
            if(!flag[edge[j].to]&&u!=v) tot--,fa[v]=u;
        }
        ans[i]=tot;
    }
    for(int i=1;i<=t+1;i++) printf("%d\n",ans[i]);
    return 0;
}

|