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

    D-City

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=4496

    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

    题意

    给你个图,他不断删边,然后问你联通块的个数

    题解:

    倒着做,然后直接并查集就好了

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)  
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int fa[maxn];
    int fi(int x)    
    {    
        return fa[x]==x?x:fa[x]=fi(fa[x]);    
    }    
      
    bool merge(int x,int y)    
    {    
        int fx=fi(x),fy=fi(y);    
        if(fx!=fy)
        {  
            fa[fx]=fy;  
            return true;  
        }  
        return false;  
    }  
    
    int ans[maxn];
    int a[maxn],b[maxn];
    int main()
    {
        //test;
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<=n;i++)
            fa[i]=i;
        for(int i=1;i<=m;i++)
            a[i]=read(),b[i]=read();
        int tot=n;
        for(int i=m;i>=1;i--)
        {
            ans[i]=tot;
            int q=fi(a[i]),p=fi(b[i]);
            if(merge(q,p))
                tot--;
        }
        for(int i=1;i<=m;i++)
            cout<<ans[i]<<endl;
        }
    }
  • 相关阅读:
    初见QT---信号和槽(二)
    初见QT---信号和槽
    Python的那些事---数据分析(一)---NumPy基础
    初见QT---创建QPushButton按钮
    初见QT---QT creator常见快捷键使用
    PHP 反射 Reflection
    python 代码求阶乘
    Python中的计时器对象
    python websocket 再线聊天室的 Demo
    Tornado创建一个web服务
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4547584.html
Copyright © 2011-2022 走看看