BZOJ 1015 JSOI2008 星球大戰 starwar 並查集

來源:互聯網
上載者:User

標籤:bzoj   bzoj1015   並查集   

題目大意:給定一個無向圖,求聯通塊個數,以及k次每次摧毀一個點後的;聯通塊個數

將邊和摧毀的點全記錄下來,反著做即可。

注意被摧毀的點不能算作聯通塊

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 400400using namespace std;struct abcd{int to,next;}table[M];int head[M],tot;int n,m,q;int fa[M],stack[M],destroy[M],top,now;bool destroyed[M];int Find(int x){if(!fa[x]||fa[x]==x)return fa[x]=x;return fa[x]=Find(fa[x]);}inline void Unite(int x,int y){int fx=Find(x);int fy=Find(y);if(fx==fy)return ;--now;fa[fy]=fx;}inline void Add(int x,int y){table[++tot].to=y;table[tot].next=head[x];head[x]=tot;}int main(){int i,j,x,y;cin>>n>>m;for(i=1;i<=m;i++){scanf("%d%d",&x,&y);++x;++y;Add(x,y);Add(y,x);}cin>>q;for(i=1;i<=q;i++){scanf("%d",&destroy[i]);++destroy[i];destroyed[destroy[i]]=1;}now=n-q;for(j=1;j<=n;j++)if(!destroyed[j])for(i=head[j];i;i=table[i].next)if(!destroyed[table[i].to])Unite(j,table[i].to);stack[++top]=now;for(j=q;j;j--){x=destroy[j];destroyed[x]=0;++now;for(i=head[x];i;i=table[i].next)if(!destroyed[table[i].to])Unite(x,table[i].to);stack[++top]=now;}while(top)printf("%d\n",stack[top--]);}


BZOJ 1015 JSOI2008 星球大戰 starwar 並查集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.