BZOJ1015 [JSOI2008] 星球大战starwar

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1015

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

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

离线处理,并查集维护连通性,将“攻击”的顺序存下,倒着加入。

最开始把加入并查集写错了……

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #define rep(i,l,r) for(int i=l; i<=r; i++)
 5 #define clr(x,y) memset(x,y,sizeof(x))
 6 #define travel(x) for(int i=last[x]; i; i=edge[i].pre)
 7 using namespace std;
 8 const int INF = 0x3f3f3f3f;
 9 const int maxn = 400010;
10 struct Edge{
11     int pre,to;
12 }edge[maxn];
13 int m,n,x,y,k,tot=0,cnt=0,last[maxn],a[maxn],ans[maxn],fa[maxn];
14 bool attack[maxn],added[maxn];
15 inline int read(){
16     int ans = 0, f = 1;
17     char c = getchar();
18     while (!isdigit(c)){
19         if (c == '-') f = -1;
20         c = getchar();
21     }
22     while (isdigit(c)){
23         ans = ans * 10 + c - '0';
24         c = getchar();
25     }
26     return ans * f;
27 }
28 inline void addedge(int x,int y){
29     edge[++tot].pre = last[x];
30     edge[tot].to = y;
31     last[x] = tot;
32 }
33 int getfa(int x){
34     return fa[x] == x ? x : fa[x] = getfa(fa[x]);
35 }
36 inline void add(int x){
37     cnt++;
38     int p = getfa(x), q;
39     travel(x) if (added[edge[i].to]){
40         q = getfa(edge[i].to);
41         if (p != q){
42             fa[q] = p;
43             cnt--;
44         }
45     }
46     added[x] = 1;
47 }
48 int main(){
49     n = read(); m = read(); clr(last,0);
50     rep(i,1,m){
51         x = read(); y = read();
52         addedge(x,y); addedge(y,x);
53     }
54     rep(i,0,n-1) fa[i] = i; clr(added,0);
55     k = read();
56     rep(i,1,k) a[i] = read(), attack[a[i]] = 1;
57     rep(i,0,n-1) if (!attack[i]) add(i);
58     ans[k+1] = cnt;
59     for(int i=k; i>=1; i--){
60         add(a[i]);
61         ans[i] = cnt;
62     }
63     rep(i,1,k+1) printf("%d\n",ans[i]);
64     return 0;
65 }
View Code

 

posted on 2015-11-30 14:22  ACMICPC  阅读(291)  评论(0编辑  收藏  举报

导航