BZOJ

1015: [JSOI2008]星球大战starwar

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 5721  Solved: 2639
[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

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

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

 

题解:逆向并查集即可,学习了新姿势,不用每次去getfa求联通块数目,只需要每次新加点的时候 num++,然后合并的时候num--即可;

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <bitset>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <cmath>
10 #include <list>
11 #include <set>
12 #include <map>
13 #define rep(i,a,b) for(int i = a;i <= b;++ i)
14 #define per(i,a,b) for(int i = a;i >= b;-- i)
15 #define mem(a,b) memset((a),(b),sizeof((a)))
16 #define FIN freopen("in.txt","r",stdin)
17 #define FOUT freopen("out.txt","w",stdout)
18 #define IO ios_base::sync_with_stdio(0),cin.tie(0)
19 #define mid ((l+r)>>1)
20 #define ls (id<<1)
21 #define rs ((id<<1)|1)
22 #define N 400010
23 #define INF 0x3f3f3f3f
24 #define INFF ((1LL<<62)-1)
25 using namespace std;
26 typedef long long LL;
27 typedef pair<int, int> PIR;
28 const double eps = 1e-8;
29  
30 int n, m, u, v, t, k[N], f[N], ans[N];
31 vector <int> G[N];
32 bool vis[N];
33 int getfa(int x){
34     return f[x] == x ? x : f[x] = getfa(f[x]);
35 }
36 int main()
37 {
38     //FIN;
39     scanf("%d %d", &n, &m);
40     rep(i, 1, m){ 
41         scanf("%d %d", &u, &v);
42         G[u].push_back(v);
43         G[v].push_back(u);
44     }
45     scanf("%d", &t);
46     rep(i, 1, t)    { scanf("%d", &k[i]); vis[k[i]] = true; }
47     rep(i, 0, n)    f[i] = i;
48  
49     int res = n-t;
50     rep(i, 0, n-1){
51         if(vis[i])  continue;
52         int x = getfa(i);
53         rep(j, 0, (int)G[i].size()-1){
54             if(vis[G[i][j]])    continue;
55             int y = getfa(G[i][j]);
56             if(x != y)  { f[y] = x; res--; }
57         }
58     }
59     /*int res = 0;
60     rep(i, 0, n-1)  { if(!vis[i] && getfa(i) == i)  res++; }*/
61     per(i, t, 0){
62         ans[i] = res++;
63         vis[k[i]] = false;
64         int x = getfa(k[i]);
65         rep(j, 0, (int)G[k[i]].size()-1){
66             if(vis[G[k[i]][j]]) continue;
67             int y = getfa(G[k[i]][j]);
68             if(x != y){ 
69                 f[y] = x;
70                 res--;
71             }
72         }
73     }
74     rep(i, 0, t)    printf("%d
", ans[i]);
75     return 0;
76 }
View Code
原文地址:https://www.cnblogs.com/Jstyle-continue/p/6363421.html