Title 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/output format
Input format:
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<n) indicates an ether tunnel between planet X and Planet Y. Note that all the Ethernet tunnels are bidirectional.
The next line is an integer k, representing the number of planets the empire plans to hit.
The next K-line is an integer X for each line, satisfying 0<=x<n, which represents the planet number that the Empire plans to strike. The empire always destroys the planets in the order in which they are entered.
Output format:
The first line of the output file is the number of connected blocks at the beginning of the planet.
The next K-line, an integer per line, indicates the number of connected blocks of the existing planet after the strike.
***************************************************************
Due to the fact that it is difficult to delete operations (not so called and check sets), we can think backwards, first to find out that all the planets have hit the end
The number of connected blocks, and then each to hit the planet Plus, at the same time again count the number of connected blocks.
The above is thought, but when the code is implemented, I think there are a few things to be aware of (especially my = = code)
1. When the ether tunnel is stored, the adjacency table in graph theory can be used to find all the tunnels connected to a point.
2. Count the number of connected blocks can be used a counter, each time adding a new point counter to add, each merge two connected block counter self-reduction.
3. The parent node initial value is set to-1, to join this node when it is changed to itself, it is better to determine which points have not been joined
END
************************
#include <cstdio>#include<algorithm>#defineMAXN 200001using namespacestd;structedge{intnum; intNext;} EDGE[MAXN*4];intHEAD[MAXN *2];intNOTE[MAXN];intSHURU[MAXN];BOOLVISITED[MAXN *2];intFATHER[MAXN *2];inttop =0;intN,m,cns,k =0;intRead () {intnum =0; Charc =GetChar (); intf =1; while(C <'0'|| C >'9') { if(c = ='-') F =-1; C=GetChar (); } while(c >='0'&& C <='9') {num*=Ten; Num+ = C-'0'; C=GetChar (); } returnNum *F;}intFindintx) { returnFATHER[X] = = x? X:FATHER[X] =find (Father[x]);}voidUnionn (intViintVJ) {Father[find (VJ)]=find (vi);}voidPushintViintVJ) {Top++; Edge[top].num=VJ; Edge[top].next=Head[vi]; HEAD[VI]=top; }voidPang (intx) {Father[x]= x; cns++; for(inti = head[x];i! =-1; i =Edge[i].next) { intj =Edge[i].num; if(Father[j]! =-1) { if(Find (j)! =find (x)) {Unionn (X,J); CNS--; } } }}intMain () {n=read (); M=read (); for(inti =0; i < N;i + +) {Father[i]= -1; Head[i]= -1; } for(inti =1; I <= M;i + +) { intVI = Read (), VJ =read (); Push (VI,VJ); Push (VJ,VI); } k=read (); for(inti =1; I <= K;i + +) {Shuru[i]=read (); Visited[shuru[i]]=true; } for(inti =0; i < N;i + +) { if(!Visited[i]) {Pang (i); }} note[0]=CNS; for(inti = K;i >=1; I--) {Pang (shuru[i]); Note[i]=CNS; } for(inti =1; I <= K;i + +) {printf ("%d\n", Note[i]); } printf ("%d", note[0]); return 0;}
Rokua P1197 [JSOI2008] Star Wars