nyist-751-破坏城市

#include<stdio.h>
using namespace std;
int temp[10005];
int find(int n)
{return temp[n]==n?n:temp[n]=find(temp[n]);}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u[100005],v[100005];
        for(int i=0;i<m;i++)
        scanf("%d%d",&u[i],&v[i]);
        for(int i=0;i<10005;i++)
            temp[i]=i;
        int num[100005];
        num[m]=n;
        for(int i=m-1;i>=0;i--)
        {
            int t,s;
            t=find(u[i]);
            s=find(v[i]);
            if(t!=s)
            {
                temp[t]=temp[s];
                num[i]=num[i+1]-1;
            }
            else
            {  num[i]=num[i+1];  }
        }
        for(int i=1;i<=m;i++)
        printf("%d\n",num[i]);
    }
    return 0;
}

 

 

 

 

反向考虑 

posted @ 2013-09-09 18:14  nylg-haozi  阅读(148)  评论(0编辑  收藏  举报