zoukankan      html  css  js  c++  java
  • 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;
        }
    }
     
  • 相关阅读:
    设备上下文-CDC绘图细节
    程序设计思想-1
    消息反射--针对通知消息
    R语言-数据类型与运算符
    背景色与WM_ERASEBKGND
    IDEA 2017.3 新版本中创建 JSF Web 应用程序缺少 web.xml 的解决办法
    在 Fedora 26/27 GNOME 3.24/3.26 环境中安装 FCITX 小企鹅输入法(修订)
    Linux 环境下安装配置 TigerVNC Server 并启用当前会话远程服务(X0VNC)
    关于 gstreamer 和 webrtc 的结合,有点小突破
    VMware Workstation/Fusion 中安装 Fedora 23/24 及其他 Linux 系统时使用 Open VM Tools 代替 VMware Tools 增强工具的方法
  • 原文地址:https://www.cnblogs.com/llsq/p/5881009.html
Copyright © 2011-2022 走看看