求解

P1197 [JSOI2008] 星球大战

DinoRun @ 2020-01-28 19:46:03

求助

#include<bits/stdc++.h>
#define read raed
using namespace std;

inline int read(void){
    int s=0,f=1;char ch=getchar();
    while(ch<'0'&&ch>'9'){if(ch=='-')f=-1;ch=getchar();};
    while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();};
    return s*f;
}

int n,m,k,nw,cnt;
int father[500005];
int ans[500005];
bool vis[500005];
struct node{
    int x,y;
}used[500005];
vector<int> nbr[500005];
stack<int> s;

inline int find(int x){
    if(father[x]==x)return x;
    return father[x]=find(father[x]);
}

int main(void){
    n=read();m=read();
    for(register int i=1;i<=m;i+=1){
        used[i].x=read();used[i].y=read();
        nbr[used[i].x].push_back(used[i].y);
        nbr[used[i].y].push_back(used[i].x);
    }
    for(register int i=0;i<=n;i+=1){
        father[i]=i;
    }
    k=read();
    cnt=n-k;
    for(register int i=1;i<=k;i+=1){
        nw=read();
        s.push(nw);
        vis[nw]=true;
    }
    for(register int i=1;i<=n;i+=1){
        if(!vis[used[i].x]&&!vis[used[i].y]){
            int nx=find(used[i].x),ny=find(used[i].y);
            if(nx!=ny){
                father[nx]=ny;
                cnt--;
            }
        }
    }
    ans[k+1]=cnt;
    for(register int q=k;q>=1;q-=1){
        nw=s.top();s.pop();vis[nw]=false;cnt++;
        for(register int i=0;i<nbr[nw].size();i+=1){
            int cur=nbr[nw][i];
            if(vis[cur])continue;
            int nx=find(nw),ny=find(cur);
            if(nx!=ny){
                father[nx]=ny;cnt--;
            }
        }
        ans[q]=cnt;
    }
    for(register int i=1;i<=k+1;i+=1){
        cout<<ans[i]<<'\n';
    }
    return 0;
}

附送评测结果 https://www.luogu.com.cn/record/29803485


by Weltschmerz @ 2020-01-28 19:48:58

一看一列的kkk头像


by Seauy @ 2020-01-28 19:51:10

先赶快改头像吧


by sun_yh @ 2020-01-28 19:59:33

头像瞩目


by Andysun06 @ 2020-01-28 20:04:16

你的头像……挺好看的


by 申佳俊 @ 2020-01-29 00:07:56

第4个for n -> m


|