BZOJ 1015 题解

1015: [JSOI2008]星球大战starwar

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
————————————————分割线————————————————
本题数据范围较大,若使用常规删边思路,基本不可做。
不妨逆向思考,这个问题可以等效为倒着加边,再用冰炸鸡维护即可。
 代码:
 1 /**************************************************************
 2     Problem: 1015
 3     User: shadowland
 4     Language: C++
 5     Result: Accepted
 6     Time:1224 ms
 7     Memory:14008 kb
 8 ****************************************************************/
 9  
10 #include "bits/stdc++.h"
11  
12 using namespace std ;
13 const int maxN = 501000 ;
14 struct Edge { int to , next ; } ;
15  
16 int head[ maxN ] , query[ maxN ] , father[ maxN ] , ans [ maxN ] ;
17 Edge e[ maxN ] ;
18 bool crash[ maxN ] , vis[ maxN ] ;
19  
20 long long tot ;
21  
22 void Init ( int N ) { for ( int i=0 ; i<=N ; ++i ) father[ i ] = i ;}
23 int getfa ( int x ) { return x ==father[ x ] ? x : father[ x ] = getfa ( father[ x ] ) ;}
24 inline void Union_Set ( const int x , const int y ) { father[ x ] = y ; }
25  
26 int INPUT ( ) {
27         int x = 0 , f = 1 ; char ch = getchar ( ) ;
28         while ( ch < '0' || ch > '9' ) { if ( ch =='-')f = -1 ; ch = getchar ( ) ;}
29         while ( ch >= '0' && ch <= '9' ) { x = ( x << 1 ) + ( x << 3 ) + ch -'0' ; ch = getchar ( ) ;}
30         return x * f ;
31 }
32  
33 int cnt ; 
34 inline void Add_Edge ( const int x , const int y ) {
35         e[ ++cnt ].to = y ; 
36         e[ cnt ].next = head[ x ] ;
37         head[ x ] = cnt ;
38 }
39  
40 void Insert ( int x ) {
41         int px = getfa ( x ) ;
42         for ( int i=head[ x ] ; i ; i = e[ i ].next ) {
43                 if ( vis[ e[ i ].to ] ) {
44                         int py = getfa ( e[ i ].to ) ;
45                         if ( px == py ) continue ;
46                         else {
47                                 --tot ;
48                                 Union_Set( py , px ) ; 
49                         }
50                 }
51         }
52 }
53  
54 int main ( ) {
55         int N = INPUT ( ) , M = INPUT ( ) ;
56         Init ( N ) ;
57         for ( int i=1 ; i<=M ; ++i ) {
58                 int _x = INPUT ( )  , _y = INPUT ( ) ;
59                 Add_Edge ( _x , _y ) ;
60                 Add_Edge ( _y , _x ) ; 
61         }
62         int D = INPUT ( ) ;
63         for ( int i=1 ; i<=D ; ++i ) {
64                 query[ i ] = INPUT ( ) ;
65                 crash[ query[ i ] ] = true ;
66         }
67         for ( int i=0 ; i<N ; ++i ) {
68                 if ( !crash[ i ] ) {
69                         ++tot ; 
70                         Insert ( i ) ;
71                         vis[ i ] = true ;
72                 }
73         }
74         ans [ D + 1 ] = tot ;
75         for ( int i=D ; i>=1 ; --i ) {
76                 ++ tot ;
77                 Insert ( query[ i ] ) ;
78                 vis[ query[ i ] ] = true ;
79                 ans [ i ] = tot ;
80         }
81         for ( int i=1 ; i<=D+1 ; ++i ) {
82                 printf ( "%d\n" , ans[ i ] ) ;
83         }
84         return 0 ;
85 }
1015

NOIP_RP++:

2016-10-11 04:03:45

(完)

 
posted @ 2016-10-11 04:04  SHHHS  阅读(297)  评论(0编辑  收藏  举报