zoukankan      html  css  js  c++  java
  • HDU -- 4496

    D-City

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


    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
    思路:并查集应用,处理是倒过来处理。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std; 
    int father[10005], a[100005], b[100005], block[100005]; 
    int find(int x)
    {
        return x == father[x] ? x : father[x] = find(father[x]); 
    }
    
    void unit(int x, int y)
    {
        int a = find(x); 
        int b = find(y); 
        father[a] = father[b]; 
        return ; 
    }
    
    int main(int argc, char const *argv[]) 
    {
        int n, m; 
        //freopen("in.c", "r", stdin); 
        while(~scanf("%d%d", &n, &m))
        {
            memset(block, 0, sizeof(block)); 
            for(int i = 0;  i < n; i ++)
                father[i] = i; 
            for(int i = 0; i < m; i ++)
                scanf("%d%d", &a[i], &b[i]);  
            for(int i = m-1; i >= 0; i --)
            {
                block[i] = n; 
                if(find(a[i]) != find(b[i]))
                {
                    n--; 
                    unit(a[i], b[i]); 
                }
            }
            for(int i = 0;i < m; i ++)
                printf("%d
    ", block[i]);
        }
        return 0; 
    }
  • 相关阅读:
    索引的结构和性能的关系
    TP5的多图上传
    TP5页面更改数字进行AJAX排序
    安装Git版本控制系统 以及Git Bash的基础命令
    tp5 前台 点击显示一个弹窗
    Tp5 (轮回) 多个富文本应用
    Tp5 (轮回) AJAX请求写搜索页面
    安装 SVN 服务器
    Tp5(轮回)------单图上传 运用AJAX 请求
    TP5中(通过一个表去取另一个表的相对应的名称)
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3603553.html
Copyright © 2011-2022 走看看