bzoj 1015: [JSOI2008]星球大战starwar 并查集

1015: [JSOI2008]星球大战starwar

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

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

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

 

题意

 

题解:

倒着并查集就好了~

直接搞

代码:

 

//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000000
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*
 
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************
const int N = 500000, M = 500000;
int key[M], head[N], next[M], cnt;
int a[N], p[N], ans[N];
bool v[N];
inline void add(const int & x, const int & y)
{
    key[cnt] = y;
    next[cnt] = head[x];
    head[x] = cnt ++;
}
inline int F (int x) 
{ 
    return p[x] == x ? x : p[x] = F (p[x]); 
}
int main ()
{
    int n, m;memset (head, -1, sizeof head);
    scanf ("%d%d", &n, &m);
    for (int i = 1, a, b; i <= m; i ++)
        scanf ("%d%d", &a, &b), add (a, b), add (b, a);int k;
    scanf ("%d", &k);
    for (int i = 1; i <= k; i ++)
        scanf ("%d", &a[i]), v[a[i]] = true;
    for (int i = 1; i <= n; i ++)
        p[i] = i;
    int z = n - k;
    for (int i = 0; i < n; i ++)
        if (!v[i])
            for (int j = head[i]; ~ j; j = next[j])
                if (!v[key[j]])
                    if (F (i) != F (key[j]))
                        p[F (i)] = F (key[j]), z --;
    ans[k + 1] = z;
    for (int i = k; i >= 1; i --)
    {
        v[a[i]] = false;    
        z ++;
        for (int j = head[a[i]]; ~ j; j = next[j])
            if (!v[key[j]])
                if (F (a[i]) != F (key[j]))
                    p[F (a[i])] = F (key[j]), z --;
        ans[i] = z;
    }
    for (int i = 1; i <= k + 1; i ++)
        printf ("%d\n", ans[i]);
    return 0;
}

 

posted @ 2015-06-12 12:58  qscqesze  阅读(450)  评论(0编辑  收藏  举报