【BZOJ 1015】[JSOI2008]星球大战starwar

1015: [JSOI2008]星球大战starwar

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

这道题得倒转过来想。

如果把一个点加进去会把多少个不同的连通块变成一个?

使用并查集的思想。加入当前点,把当前点连接的点的连通块变成一个就行了,没有过多技巧。

#include<cstdio>
#include<cstring>
 
using namespace std;
 
const int MAXM = 400005;
const int MAXN = MAXM << 1;
 
struct Edge
{
    int from;
    int to;
    int next;
     
    Edge() {
    }
     
    Edge(int u, int v) : from(u), to(v) {
    }
};
 
Edge edges[MAXM << 1];
int n;
int m;
int tots;
int first[MAXN];
int f[MAXN];
int x;
int y;
int qest[MAXN];
int q;
int ans[MAXN];
int nblock;
bool v[MAXN];
 
void add(int u, int v)
{
    edges[tots] = Edge(u, v);
    edges[tots].next = first[u];
    first[u] = tots++;
}
 
int getf(int x)
{
    if (x == f[x]) return x;
    f[x] = getf(f[x]);
    return f[x];
}
 
int main()
{
    scanf("%d%d", &n, &m);
    memset(first, -1, sizeof(first));
    tots = 0;
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d", &x, &y);
        add(x, y);
        add(y, x);
    }
    memset(v, true, sizeof(v));
    scanf("%d", &q);
    for (int i = 0; i < q; ++i) scanf("%d", &qest[i]), v[qest[i]] = false;
    for (int i = 0; i < n; ++i) f[i] = i;
    for (int i = 0; i < n; ++i)
        if (v[i])
            for (int j = first[i]; j != -1; j = edges[j].next)
            {
                Edge &e = edges[j];
                if (v[e.to])
                {
                    int tx = getf(i);
                    int ty = getf(e.to);
                    f[ty] = tx;
                }
            }
    nblock = 0;
    for (int i = 0; i < n; ++i)
        if (f[i] == i && v[i]) nblock++;
    for (int i = q - 1; i >= 0; --i)
    {
        ans[i] = nblock;
        nblock++;
        v[qest[i]] = true;
        for (int j = first[qest[i]]; j != -1; j = edges[j].next)
        {
            Edge &e = edges[j];
            if (v[e.to])
            {
                y = getf(qest[i]);
                x = getf(e.to);
                if (x != y)
                {
                    f[y] = x;
                    nblock--;
                }
            }
        }
    }
    printf("%d
", nblock);
    for (int i = 0; i < q; ++i) printf("%d
", ans[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/albert7xie/p/4761867.html