• hdu-4496-D-City


    D-City

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 3838    Accepted Submission(s): 1379


    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.
     
    题目大意:逆行并查集
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496
     
    递归代码:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int MAX=1e4+10;
    const int MAX1=1e5+10;
    int f[MAX];
    int sum[MAX1];
    struct ss
    {
        int a,b;
    }s[MAX1];
    int find(int n)
    {
        if(n!=f[n])
        f[n]=find(f[n]);
        return f[n];
    }
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            for(int i=0;i<n;i++)
            f[i]=i;
            for(int i=0;i<m;i++)
            scanf("%d%d",&s[i].a,&s[i].b);
            sum[m]=n;
            for(int i=m-1;i>=0;i--)
            {
    
                int a1=find(s[i].a);
                int a2=find(s[i].b);
                if(a1!=a2)
                {f[a1]=a2;sum[i]=sum[i+1]-1;}
                else
                sum[i]=sum[i+1];
            }
           for(int i=1;i<=m;i++)
           cout<<sum[i]<<endl;
        }
    }

    递推代码:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int MAX=1e4+10;
    const int MAX1=1e5+10;
    int f[MAX];
    int sum[MAX1];
    struct ss
    {
        int a,b;
    }s[MAX1];
    int find(int n)
    {
        int res=n;
        while(n!=f[n])
        n=f[n];
        int j;
        while(res!=n)
        {
            j=f[res];
            f[res]=n;
            res=j;
        }
        return n;
    }
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            for(int i=0;i<n;i++)
            f[i]=i;
            for(int i=0;i<m;i++)
            scanf("%d%d",&s[i].a,&s[i].b);
            sum[m]=n;
            for(int i=m-1;i>=0;i--)
            {
    
                int a1=find(s[i].a);
                int a2=find(s[i].b);
                if(a1!=a2)
                {f[a1]=a2;find(a1);sum[i]=sum[i+1]-1;}
                else
                sum[i]=sum[i+1];
            }
           for(int i=1;i<=m;i++)
           cout<<sum[i]<<endl;
        }
    }
     
  • 相关阅读:
    超越自我的事
    ENVI/SARscape软件概述及安装
    《万万没想到:用理工科思维理解世界》读书简记
    Ruby on Rails 搭建环境 (ubuntu)
    拓荒者
    Rails 画类图的几个方法
    ERROR: Error installing mysql2: ERROR: Failed to build gem native extension [@Ubuntu 15.04]
    无题20150105
    关于Unity中Camera的Aspect
    Esfog_UnityShader教程_逐帧动画
  • 原文地址:https://www.cnblogs.com/llsq/p/5881009.html
Copyright © 2020-2023  润新知