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 has given you a mission: to give the number of connected blocks of the planet that the rebel forces occupy after each strike, by giving the etheric tunnel connectivity between the original two planets and the Order of the empire-hit planets, as fast 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) andM1<< Span class= "Mrel" >=m<=2< Span class= "Mord" >00,00 0 ), respectively, representing the number of planets and the number of etheric tunnels. Planet with 0 ~ n-1 integer number.
The nextMRows, each row consists of two integersX,Y , where ( 0<=< Span class= "Mord mathit" >x<>y represents the planet x and Planet y< The span class= "Mord mathit" is a "ether" tunnel that can be communicated directly.
The next behavior is an integer &NBSP; Represents the number of planets that will be hit.
Next k< Span class= "Strut bottom" > line, each line has an integer, in order to list the attack target of imperial army. This k The number is different, and all in 0< Span class= "Mord" to n ? 1 .
Output format:
The first line 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.
Input and Output Sample input example # #:
8 130 11 66 55 00 61 22 33 44 57 17 27 63 6516357
Sample # # of output:
111233
Description
[JSOI2008]
The--LVYOUYW is not as good as adding edge.
When we see the Unicom block, we are bright, the mind flashed and check set. But how do we maintain the operation of each deletion, it looks very complex look.
We might as well change the idea, first of all, the end of all sides have been destroyed, but also to find the last case of the answer, a little bit of add edge. Because we will add edge operation, do not delete edge operation Ah! So, this offline algorithm is determined.
Concrete operation: The broken edge, the edge, we sound like a graph theory, but in the subject matter is in and check the concentration of their merger/dismantling. In the first case, we use the adjacency table to save the information and check the set maintenance.
In this way, when we read the message that we need to break the edge, we keep recording it silently. We must first realize that the addition of the edge of the block, the number of broken edge unicom increased. This way we can traverse the node that needs to be broken backwards and traverse all the edges that are connected to it.
Code
1#include <cstdio>2#include <algorithm>3 4 using namespacestd;5 6 intn,m,cnt,tot,x,y,k,head[400090],ans[400090],f[400090],flag[400090],mark[400090];7 structnode{8 intTo,next, from;9}edge[400090];Ten One intGETF (intx) A { - if(f[x]==x)returnF[x]; - returnf[x]=GETF (f[x]); the } - - voidMergeintXinty) - { + intpp=GETF (x); - intqq=getf (y); + if(PP!=QQ) f[qq]=pp; A } at - voidAddintXinty) - { -edge[++cnt].to=y; -EDGE[CNT]. from=x;//Unlike normal adjacency tables, we maintain a message here. -edge[cnt].next=Head[x]; inhead[x]=CNT; - } to + intMain () - { thescanf"%d%d",&n,&m); * for(intI=0; i<=n;i++) f[i]=i,head[i]=-1; $ //initialization is important! Node number starting at 0, head set to-1Panax Notoginseng //also note the location of the initialization (funny - for(intI=1; i<=m;i++) the { +scanf"%d%d",&x,&y); A Add (x, y); the Add (y,x); + } -scanf"%d",&k); $ for(intI=1; i<=k;i++) $ { -scanf"%d",&x); -flag[x]=1; themark[i]=x; - }WuyiTot=n-k;//start each point as an independent unicom block the for(intI=1; i<=2*m;i++) - { Wu if(!flag[edge[i].to]&&!flag[edge[i]. from]&&GETF (edge[i].to)!=getf (Edge[i]. from)) -Merge (Edge[i].to,edge[i]. from), tot--; About //On each side, the Unicom block will be reduced a little bit $ } -ans[k+1]=tot; - for(inti=k;i>=1; i--) - { Atot++;intp=mark[i];flag[p]=0; + //The point at which the current is not connected is a separate unicom block . the for(intj=head[p];j!=-1; j=edge[j].next) -{//head Change, here it is, notice! $ if(!FLAG[EDGE[J].TO]&&GETF (edge[j].to)! =GETF (p)) thetot--, merge (edge[j].to,p); the } theans[i]=tot; the } - for(intI=1; i<=k+1; i++) printf ("%d\n", Ans[i]); in return 0; the}
View Code
Attached: and check set to find the number of Unicom blocks
77622043
Luogu P1197 [JSOI2008] Star Wars by cellur925