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;
        }
    }
     
  • 相关阅读:
    DJango简单的后台定义登录验证
    简单聊聊HTTP/TCP/IP协议
    简单的线程说明
    设计模式 -- 常用设计模式
    网络知识 -- 第二部
    c#利用脚本,本地执行linux命令
    Json和类之间的转化
    关于地址映射穿透和套接字复用的说明
    多线程调用中的注意事项
    Task多线程的常规用法
  • 原文地址:https://www.cnblogs.com/llsq/p/5881009.html
Copyright © 2011-2022 走看看