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
",num[i]);
    }
    return 0;
}

反向考虑 

原文地址:https://www.cnblogs.com/nylg-haozi/p/3310366.html