1015: [JSOI2008] Star Wars Starwar time limit:3 Sec Memory limit:162 MB
submit:3685 solved:1641
[Submit] [Status] [Discuss] 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
The problem has to be reversed.
How many different connected blocks can be turned into one if one point is added?
The idea of using and checking the set. Join the current point and turn the connecting block of the point connected at the current point into a single line, without too much skill.
#include <cstdio>#include<cstring>using namespacestd;Const intMAXM =400005;Const intMAXN = Maxm <<1; structedge{int from; intto ; intNext; Edge () {} Edge (intUintV): from(U), to (v) {}}; Edge EDGES[MAXM<<1];intN;intm;intTots;intFIRST[MAXN];intF[MAXN];intx;inty;intQEST[MAXN];intQ;intANS[MAXN];intNblock;BOOLV[MAXN];voidAddintUintv) {Edges[tots]=Edge (U, v); Edges[tots].next=First[u]; First[u]= tots++;} intGETF (intx) { if(x = = F[x])returnx; F[X]=GETF (f[x]); returnf[x];} intMain () {scanf ("%d%d", &n, &m); memset (First,-1,sizeof(first)); Tots=0; for(inti =0; I < m; ++i) {scanf ("%d%d", &x, &y); Add (x, y); Add (y, x); } memset (V,true,sizeof(v)); scanf ("%d", &q); for(inti =0; i < Q; ++i) scanf ("%d", &qest[i]), v[qest[i]] =false; for(inti =0; I < n; ++i) F[i] =i; for(inti =0; I < n; ++i)if(V[i]) for(intj = First[i]; J! =-1; j =Edges[j].next) {Edge&e =Edges[j]; if(V[e.to]) {inttx =GETF (i); intTy =GETF (e.to); F[ty]=TX; }} Nblock=0; for(inti =0; I < n; ++i)if(F[i] = = i && v[i]) nblock++; for(inti = q-1; I >=0; --i) {ans[i]=Nblock; Nblock++; V[qest[i]]=true; for(intj = First[qest[i]]; J! =-1; j =Edges[j].next) {Edge&e =Edges[j]; if(v[e.to]) {y=GETF (Qest[i]); X=GETF (e.to); if(X! =y) {f[y]=x; Nblock--; } }}} printf ("%d\n", Nblock); for(inti =0; i < Q; ++i) printf ("%d\n", Ans[i]); return 0;}
"Bzoj 1015" [JSOI2008] Star Wars Starwar