• [BZOJ 1015] [JSOI 2008] 星球大战starwar


    1015: [JSOI2008]星球大战starwar

    Time Limit: 3 Sec
    Memory Limit: 162 MB

    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

    【题解】

    本题数据范围一看,不能用tarjan,我也不会用TAT

    于是就用并查集

    看了看在在线搞好像会超时,然后就想到了离线来做。

    先连着没有拆掉的边,然后求求连通块,倒过来做

    每次删一条边倒过来就是添一条边。

    然后并查集即可。

     1 #include<stdio.h>
     2 using namespace std;
     3 int a[400001],b[400001],c[400001],pre[400001],head[400001],next[400001],to[400001],tot,n,m,k;
     4 bool des[400001];
     5 int ans[400001];
     6 int getf(int x) {
     7     int r=x;
     8     while(r!=pre[r]) r=pre[r];
     9     int i=x,j;
    10     while(i!=r) {
    11         j=pre[i];
    12         pre[i]=r;
    13         i=j;
    14     }
    15     return r;
    16 }
    17 void addedge(int u,int v) {
    18     ++tot;
    19     to[tot]=v;
    20     next[tot]=head[u];
    21     head[u]=tot;
    22 }
    23 int main() {
    24     scanf("%d%d",&n,&m);
    25     for (int i=1;i<=n;++i) pre[i]=i;
    26     for (int i=1;i<=m;++i) {
    27         scanf("%d%d",&a[i],&b[i]);
    28         a[i]++,b[i]++;
    29         addedge(a[i],b[i]);
    30         addedge(b[i],a[i]);
    31     }
    32     scanf("%d",&k);
    33     for (int i=1;i<=k;++i) {
    34         scanf("%d",&c[i]);
    35         c[i]++;
    36         des[c[i]]=1;
    37     }
    38     int ansx=n-k;
    39     for (int i=1;i<=n;++i) 
    40         if(!des[i]) {
    41             int f1=getf(i);
    42             for (int j=head[i];j;j=next[j]) {
    43                 if(!des[to[j]]) {
    44                     int f2=getf(to[j]);
    45                     if(f1!=f2) pre[f2]=f1, ansx--;
    46                 }
    47             }
    48         }
    49     ans[k+1]=ansx;
    50     for (int i=k;i>=1;--i) {
    51         ++ansx;
    52         int f1=c[i];
    53         for (int j=head[c[i]];j;j=next[j]) 
    54             if(!des[to[j]]) {
    55                 int f2=getf(to[j]);
    56                 if(f1!=f2) pre[f2]=f1,--ansx;
    57             }
    58         ans[i]=ansx;
    59         des[c[i]]=0;
    60     }
    61     for (int i=1;i<=k+1;++i) printf("%d
    ",ans[i]);
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    SharedPreferences
    Handler
    Gallery 和ImageSwitcher
    poj 1077 Eight (BFS)
    HDU 1208 Pascal's Travels( 记忆化搜索)
    HDU 1619 Unidirectional TSP (dp,dfs)
    HDU 3683 Gomoku (枚举+BFS)
    HDU 3647 Tetris (暴力DFS)
    poj 1020 Anniversary Cake (DFS)
    poj 1375 Intervals(解析几何 过圆外一点求与圆的切线)
  • 原文地址:https://www.cnblogs.com/TonyNeal/p/bzoj1015.html
Copyright © 2020-2023  润新知