1015: [JSOI2008] Star Wars Starwar time limit:3 Sec Memory limit:162 MB
Description
Long ago, in a distant galaxy, a dark empire was leaning against its super-weapons rulers throughout the galaxy. One day, with an occasional chance, a rebel army destroyed the empire's super weapons and attacked almost all the planets in the galaxy. These planets are connected to each other directly or indirectly through special etheric tunnels. 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 plan 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 leader gives you a mission: to give the connection of the etheric tunnels between the original two planets and the order of the planets in the Empire's attack, to find out the number of connected fast planets that the rebels occupy after each strike as quickly as possible. (If two planets can be connected directly or indirectly through existing etheric channels, the two planets are in the same connected block).
Input
The first line of the input file contains two integers, n (1 <= n <= 2M) and M (1 <= m <= 200,000), respectively, representing the number of planets and the number of etheric tunnels. Planets are numbered with 0~n-1 integers. The next M-line, each line consists of two integers X, Y, where (0<=x<>y
Output
The first line of the output file is the number of connected blocks at the beginning of the planet. The next n rows, one integer per line, indicate the number of connected blocks of the existing planet after the strike.
Sample Input8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
9 |
7 6
3 6
5
1
6
3
5
7Sample Output1
1
1
2
3
3HINT
Exercises
The scope of the subject data a look, can not use Tarjan, I will not use tat
So I used and looked up the set
Look at the online good like will time out, and then think of offline to do.
Connect the edges without dismantling, then seek the connected block, and do it upside down.
Every time you delete an edge, you add an edge.
And then you can check the set.
1#include <stdio.h>2 using namespacestd;3 inta[400001],b[400001],c[400001],pre[400001],head[400001],next[400001],to[400001],tot,n,m,k;4 BOOLdes[400001];5 intans[400001];6 intGETF (intx) {7 intR=x;8 while(R!=pre[r]) r=Pre[r];9 intI=x,j;Ten while(i!=r) { Onej=Pre[i]; Apre[i]=R; -I=J; - } the returnR; - } - voidAddedge (intUintv) { -++tot; +to[tot]=v; -next[tot]=Head[u]; +head[u]=tot; A } at intMain () { -scanf"%d%d",&n,&m); - for(intI=1; i<=n;++i) pre[i]=i; - for(intI=1; i<=m;++i) { -scanf"%d%d",&a[i],&b[i]); -a[i]++,b[i]++; in Addedge (A[i],b[i]); - Addedge (B[i],a[i]); to } +scanf"%d",&k); - for(intI=1; i<=k;++i) { thescanf"%d",&c[i]); *c[i]++; $des[c[i]]=1;Panax Notoginseng } - intansx=n-K; the for(intI=1; i<=n;++i) + if(!Des[i]) { A intf1=GETF (i); the for(intj=head[i];j;j=Next[j]) { + if(!Des[to[j]]) { - intF2=GETF (To[j]); $ if(F1!=F2) Pre[f2]=f1, ansx--; $ } - } - } theans[k+1]=ansx; - for(inti=k;i>=1;--i) {Wuyi++ansx; the intf1=C[i]; - for(intj=head[c[i]];j;j=Next[j]) Wu if(!Des[to[j]]) { - intF2=GETF (To[j]); About if(F1!=F2) Pre[f2]=f1,--ansx; $ } -ans[i]=ansx; -des[c[i]]=0; - } A for(intI=1; i<=k+1; ++i) printf ("%d\n", Ans[i]); + return 0; the}
View Code
[Bzoj 1015] [Jsoi 2008] Star Wars Starwar