BZOJ 1015 题解

时间:2023-03-09 20:47:03
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
————————————————分割线————————————————
本题数据范围较大,若使用常规删边思路,基本不可做。
不妨逆向思考,这个问题可以等效为倒着加边,再用冰炸鸡维护即可。
 代码:
 /**************************************************************
Problem: 1015
User: shadowland
Language: C++
Result: Accepted
Time:1224 ms
Memory:14008 kb
****************************************************************/ #include "bits/stdc++.h" using namespace std ;
const int maxN = ;
struct Edge { int to , next ; } ; int head[ maxN ] , query[ maxN ] , father[ maxN ] , ans [ maxN ] ;
Edge e[ maxN ] ;
bool crash[ maxN ] , vis[ maxN ] ; long long tot ; void Init ( int N ) { for ( int i= ; i<=N ; ++i ) father[ i ] = i ;}
int getfa ( int x ) { return x ==father[ x ] ? x : father[ x ] = getfa ( father[ x ] ) ;}
inline void Union_Set ( const int x , const int y ) { father[ x ] = y ; } int INPUT ( ) {
int x = , f = ; char ch = getchar ( ) ;
while ( ch < '' || ch > '' ) { if ( ch =='-')f = - ; ch = getchar ( ) ;}
while ( ch >= '' && ch <= '' ) { x = ( x << ) + ( x << ) + ch -'' ; ch = getchar ( ) ;}
return x * f ;
} int cnt ;
inline void Add_Edge ( const int x , const int y ) {
e[ ++cnt ].to = y ;
e[ cnt ].next = head[ x ] ;
head[ x ] = cnt ;
} void Insert ( int x ) {
int px = getfa ( x ) ;
for ( int i=head[ x ] ; i ; i = e[ i ].next ) {
if ( vis[ e[ i ].to ] ) {
int py = getfa ( e[ i ].to ) ;
if ( px == py ) continue ;
else {
--tot ;
Union_Set( py , px ) ;
}
}
}
} int main ( ) {
int N = INPUT ( ) , M = INPUT ( ) ;
Init ( N ) ;
for ( int i= ; i<=M ; ++i ) {
int _x = INPUT ( ) , _y = INPUT ( ) ;
Add_Edge ( _x , _y ) ;
Add_Edge ( _y , _x ) ;
}
int D = INPUT ( ) ;
for ( int i= ; i<=D ; ++i ) {
query[ i ] = INPUT ( ) ;
crash[ query[ i ] ] = true ;
}
for ( int i= ; i<N ; ++i ) {
if ( !crash[ i ] ) {
++tot ;
Insert ( i ) ;
vis[ i ] = true ;
}
}
ans [ D + ] = tot ;
for ( int i=D ; i>= ; --i ) {
++ tot ;
Insert ( query[ i ] ) ;
vis[ query[ i ] ] = true ;
ans [ i ] = tot ;
}
for ( int i= ; i<=D+ ; ++i ) {
printf ( "%d\n" , ans[ i ] ) ;
}
return ;
}

1015

NOIP_RP++:

2016-10-11 04:03:45

(完)