1015: [JSOI2008] Star Wars Starwar
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. The planet is numbered with an integer of 0 ~ N-1. The next M-line, each line consists of two integers x, y, where (0 < = x <> y means there is an "ether" tunnel between Planet X and Planet Y, which can be communicated directly. The next behavior is an integer k, which represents the number of planets that will be hit. The next K-line, each line has an integer, in order to list the attack target of the imperial army. The k numbers are different and are within the range of 0 to n-1.
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 Source
The puzzle: It's just a little bit more when you rewind it offline.
//Meek///#include <bits/stdc++.h>#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<cstring>#include<algorithm>#include<queue>#include<map>#include<Set>#include<stack>#include<sstream>#include<vector>using namespacestd;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairtypedefLong Longll;Const intN =510000;Const intINF =99999999;Const intMod=1000000007;intN,m,k[n],q,num,parent[n],done[n];vector<int>G[n],ans;voidinit () {mem (done); Num=N; for(intI=0; i<=n;i++) parent[i]=i;}intFindsintx) {if(x! = Parent[x])returnPARENT[X] =finds (parent[x]); Else returnx;}voidUnion (intXinty) {intFX =finds (x); intFY =finds (Y); if(FX! =FY) {num--; PARENT[FY]=FX; }}intMain () {scanf ("%d%d",&n,&m); Init ();intx, y; for(intI=1; i<=m;i++) {scanf ("%d%d",&x,&y); G[X].PB (y); G[Y].PB (x); } scanf ("%d",&q); for(intI=1; i<=q;i++) {scanf ("%d",&K[i]); Done[k[i]]=1; } for(intI=0; i<n;i++) { if(!Done[i]) { for(intj=0; J<g[i].size (); j + +) { intto=G[i][j]; if(!Done[to]) {Union (i,to); } } } } //cout<<num<<endl; for(inti=q;i>=1; i--) {ANS.PB (num-i); for(intj=0; J<g[k[i]].size (); j + +) { intto=G[k[i]][j]; if(!Done[to]) {Union (k[i],to); }} Done[k[i]]=0; } ANS.PB (num); for(intI=ans.size ()-1; i>=0; i--) {printf ("%d\n", Ans[i]); } return 0;}
Code
Bzoj 1015: [JSOI2008] Star Wars Starwar and check set