1015: [JSOI2008]星球大战starwar

Time Limit: 3 Sec  Memory Limit: 162 MB

Submit: 6784  Solved: 3180

[​​Submit​​​][​​Status​​​][​​Discuss​​]

Description

  很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系。某一天,凭着一个偶然的
机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球。这些星球通过特殊的以太隧道互相直
接或间接地连接。 但好景不长,很快帝国又重新造出了他的超级武器。凭借这超级武器的力量,帝国开始有计划
地摧毁反抗军占领的星球。由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来。现在,反抗军首
领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽量快的速度求出每
一次打击之后反抗军占据的星球的连通快的个数。(如果两个星球可以通过现存的以太通道直接或间接地连通,则
这两个星球在同一个连通块中)。

Input

  输入文件第一行包含两个整数,N (1  < =  N  < =  2M) 和M (1  < =  M  < =  200,000),分别表示星球的
数目和以太隧道的数目。星球用 0 ~ N-1的整数编号。接下来的M行,每行包括两个整数X, Y,其中(0 < = X <> 
Y 表示星球x和星球y之间有“以太”隧道,可以直接通讯。接下来的一行为一个整数k,表示将遭受攻击的星球的
数目。接下来的k行,每行有一个整数,按照顺序列出了帝国军的攻击目标。这k个数互不相同,且都在0到n-1的范
围内。

Output

第一行是开始时星球的连通块个数。接下来的K行,每行一个整数,表示经过该次打击后现存星球
的连通块个数。

Sample Input

8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7

Sample Output

1
1
1
2
3
3

HINT

 

Source

正着不断删点做不了,很巧妙的想法,存入询问,然后倒着插点进去

1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=4e5+5;
5 int n,m,K;
6 int tot,head[MAX],adj[MAX],next[MAX];
7 int fa[MAX],ans[MAX],re[MAX],tt;
8 bool vis[MAX],t[MAX];
9 inline int read(){
10 int an=0,x=1;char c=getchar();
11 while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
12 while (c>='0' && c<='9') {an=(an<<3)+(an<<1)+c-'0';c=getchar();}
13 return an*x;
14 }
15 void addedge(int u,int v){
16 tot++;adj[tot]=v,next[tot]=head[u],head[u]=tot;
17 }
18 int getfather(int x){return fa[x]==x?x:fa[x]=getfather(fa[x]);}
19 void add(int x){
20 int tx=getfather(x),ty,i;
21 for (i=head[x];i;i=next[i]){
22 if (vis[adj[i]]){
23 ty=getfather(adj[i]);
24 if (tx!=ty) fa[ty]=tx,tt--;
25 }
26 }
27 }
28 int main(){
29 freopen ("planet.in","r",stdin);freopen ("planet.out","w",stdout);
30 int i,j,u,v;
31 n=read(),m=read();tot=1;
32 for (i=1;i<=m;i++){
33 u=read(),v=read();u++,v++;
34 addedge(u,v),addedge(v,u);
35 }
36 for (i=1;i<=n;i++) fa[i]=i;
37 K=read();
38 for (i=1;i<=K;i++){
39 re[i]=read(),re[i]++;
40 t[re[i]]=true;
41 }
42 for (i=1;i<=n;i++)
43 if (!t[i]){
44 tt++;
45 add(i);
46 vis[i]=true;
47 }
48
49 ans[K+1]=tt;
50 for (i=K;i>=1;i--){
51 tt++;
52 add(re[i]);
53 vis[re[i]]=true;
54 ans[i]=tt;
55 }
56 for (i=1;i<=K+1;i++)
57 printf("%d\n",ans[i]);
58 return 0;
59