zoukankan      html  css  js  c++  java
  • hdu 4496 D-City(并查集)

    Problem Description
    Luxer is a really bad guy. He destroys everything he met. 
    One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input. 
    Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
    Input
    First line of the input contains two integers N and M. 
    Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line. 
    Constraints: 
    0 < N <= 10000 
    0 < M <= 100000 
    0 <= u, v < N. 
     
    Output
    Output M lines, the ith line is the answer after deleting the first i edges in the input.
     
    Sample Input
    5 10 
    0 1 
    1 2 
    1 3 
    1 4 
    0 2 
    2 3 
    0 4 
    0 3 
    3 4 
    2 4
     
    Sample Output
    1 
    1 
    1 
    2 
    2 
    2 
    2 
    3 
    4 
    5

    Hint
    The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there's only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.
     
    Source
     

    题目的意思是: 一开始有个图(n个点 m条边),然后从第0条边开始删除,(0~m条边依次删除),然后每删除一条边统计依次有多少个连通块。

    做法是: 直接从最后一条边开始枚举,依次往前面添加合并边,统计连通块即可。具体看代码,应该看得懂。

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 10006
    23 #define M 100006
    24 #define inf 1e12
    25 int n,m;
    26 int fa[N],ans[M];
    27 int a[M],b[M];
    28 void init(){
    29    for(int i=0;i<N;i++){
    30       fa[i]=i;
    31    }
    32 }
    33 int find(int x){
    34    return fa[x]==x?x:fa[x]=find(fa[x]);
    35 }
    36 bool merge(int x,int y){
    37    int root1=find(x);
    38    int root2=find(y);
    39    if(root1==root2) return false;
    40    fa[root1]=root2;
    41    return true;
    42 }
    43 int main()
    44 {
    45    while(scanf("%d%d",&n,&m)==2){
    46       init();
    47       for(int i=0;i<m;i++){
    48          scanf("%d%d",&a[i],&b[i]);
    49       }
    50       ans[m-1]=n;
    51       int k=m-1;
    52       for(int i=m-1;i>=0;i--){
    53          if(merge(a[i],b[i])){
    54             ans[k-1]=ans[k]-1;
    55          }else{
    56             ans[k-1]=ans[k];
    57          }
    58          k--;
    59       }
    60       for(int i=0;i<m;i++){
    61          printf("%d
    ",ans[i]);
    62       }
    63 
    64    }
    65 
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    60款很酷的 jQuery 幻灯片演示和下载
    分享27款非常棒的 jQuery 表单插件
    10款新鲜出炉的 jQuery 插件
    5 个很炫的 HTML5 游戏
    12款高质量的免费 HTML 网页模板下载
    jquery插件整理篇(四)自动补全类插件
    jquery插件整理篇(三)图片展示插件
    超强1000个jquery插件!
    34个漂亮的应用程序后台管理系统界面(系列二)
    jquery插件整理篇(九)数据验证类
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4995517.html
Copyright © 2011-2022 走看看