BZOJ_1015_[JSOI2008] Star Wars _ and check set
Test instructions: Long ago, in a distant galaxy, a dark empire was leaning against its super-weapons ruler throughout the galaxy. One day, with an accidental
Opportunity, a rebel army destroyed the empire's super weapons and attacked almost all the planets in the galaxy. These planets are directly connected to each other through special etheric tunnels.
Connected to or indirectly. But it was not long, and soon the Empire re-created his super weapon. With the power of this super weapon, the Empire began to have plans
To destroy the rebel-occupied planet. As the planet continues to be destroyed, communication channels between the two planets are beginning to be unreliable. Now, the rebel army's first
I'll give you a mission: provide an Ethernet tunnel connection between the original two planets and the Order of the empire-hit planets to find out every
The number of connected fast planets that the rebels occupy after a strike. (If two planets can be connected directly or indirectly through the existing etheric channels,
The two planets are in the same connected block).
Analysis: Offline, delete as insert, each use and check set maintenance connected block number.
Code:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 #defineN 4000506 intn,fa[n],m,q[n],ans[n],cnt;7 inthead[n],to[n<<1],nxt[n<<1],vis[n],k;8InlinevoidAddintUintv)9 {Tento[++cnt]=v;nxt[cnt]=head[u];head[u]=CNT; One } A intFindintx) - { - intp=Fa[x]; the while(P!=fa[p]) p=Fa[p]; - while(x!=2) { - inttmp=Fa[x]; -fa[x]=p; +x=tmp; - } + returnFa[x]; A } at intMain () - { -Registerinti,j; - intx, y; -scanf"%d%d",&n,&m); - for(i=1; i<=n;i++) fa[i]=i; in for(i=1; i<=m;i++){ -scanf"%d%d",&x,&y); tox++,y++; + Add (x, y); add (y,x); - } the for(i=1; i<=n;i++) vis[i]=1; *scanf"%d",&k); $ans[k]=n-K;Panax Notoginseng for(i=1; i<=k;i++){ -scanf"%d",&q[i]); theq[i]++; +vis[q[i]]=0; A } the for(i=1; i<=n;i++){ + if(!vis[i])Continue; - for(j=head[i];j;j=Nxt[j]) { $ if(!vis[to[j]])Continue; $ intDj=find (To[j]), di=find (i); - if(di!=DJ) { -fa[di]=dj;ans[k]--; the } - }Wuyi } thevis[q[k]]=1; - for(i=k-1; i>=0; i--){ Wu intx=q[i+1]; -ans[i]=ans[i+1]+1; About for(j=head[x];j;j=Nxt[j]) { $ intt=To[j]; - if(!vis[t])Continue; - intDy=find (t), dx=find (x); - if(dx!=dy) { Afa[dy]=dx;ans[i]--; + } the } -vis[q[i]]=1; $ } the for(i=0; i<=k;i++){ theprintf"%d\n", Ans[i]); the } the}
BZOJ_1015_[JSOI2008] Star Wars _ and check set