• BZOJ 1015: [JSOI2008]星球大战starwar( 并查集 )


    并查集离线搞.... 

    ------------------------------------------------------------------------------------ 

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<iostream>
     
    #define rep( i , n ) for( int i = 0 ; i < n ; ++i )
    #define clr( x , c ) memset( x , c ,sizeof( x ) )
     
    using namespace std;
     
    const int maxn = int( 4e5 ) + 5;
     
    int p[ maxn ];
     
    int n;
     
    int find( int x ) {
    return x == p[ x ] ? x : p[ x ] = find( p[ x ] );
    }
     
    stack< int > S;
    vector< int > ans;
    vector< int > G[ maxn ];
    bool vis[ maxn ];
    bool B[ maxn ];
    int tot = 0;
     
    void init() {
    rep( i , n ) 
       p[ i ] = i , G[ i].clear();
    clr( vis , 0 );
    clr( B , 0 );
    ans.clear();
    }
     
    void unite( int x ) {
    tot++;
    rep( i , G[ x ].size() ) {
    int t = G[ x ][ i ];
    if( ! vis[ t ] ) continue;
    int a = find( x ) , b = find( t );
    if( a != b )
       tot-- , p[ a ] = b;
       
    }
    vis[ x ] = true;
    }
     
    int main() {
    int m;
    cin >> n >> m;
    init();
    while( m-- ) {
    int u , v;
    scanf( "%d%d" , &u , &v );
    G[ u ].push_back( v );
    G[ v ].push_back( u );
    }
    cin >> m;
    while( m-- ) {
    int t;
    scanf( "%d" , &t );
    S.push( t );
    B[ t ] = true;
    }
    rep( i , n ) 
       if( ! B[ i ] ) unite( i );
    while( ! S.empty() ) {
    int x = S.top();
    S.pop();
    ans.push_back( tot );
    unite( x );
    }
    ans.push_back( tot );
    for( int i = ans.size() - 1 ; i >= 0 ; i-- )
    printf( "%d " , ans[ i ] );
    return 0;
    }

    ------------------------------------------------------------------------------------

    1015: [JSOI2008]星球大战starwar

    Time Limit: 3 Sec  Memory Limit: 162 MB
    Submit: 3375  Solved: 1489
    [Submit][Status][Discuss]

    Description

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

    Input

    输入文件第一行包含两个整数,N (1 <= N <= 2M) 和M (1 <= M <= 200,000),分别表示星球的数目和以太隧道的数目。星球用0~N-1的整数编号。接下来的M行,每行包括两个整数X, Y,其中(0<=X<>Y

    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

    HINT

    Source

  • 相关阅读:
    [整理]正睿 CSP 七连测划水
    关于我想了好久才想出这题咋做这档事
    关于我想了好久才想出这题咋做这档事
    关于我想了好久才想出这题咋做这档事
    [考试总结]ZROI-21-NOIP冲刺-TEST11 总结
    关于我想了很久才想出这题咋做这档事
    [题解]POJ3415 Common Substrings
    [题解]CF1073G Yet Another LCP Problem
    [题解]Codeforces Round #751 (Div. 2) 题解
    [考试总结]ZROI-21-NOIP冲刺-TEST10 总结
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4541526.html
Copyright © 2020-2023  润新知