逆序并查集
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define x first #define y second #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define up rt,rt<<1,rt<<1|1 #define mem(x) memset(x,0,sizeof(x)) #define mem1(x) memset(x,-1,sizeof(x)) #define LMissher using namespace std; typedef long long ll; typedef double db; const int M = 1e5+7; const double pi = acos(-1); const int inf = 2147483647; const int mod = 1e9+7; int n,m; int f[M],ans[M]; struct edge { int u,v; }e[M]; void init(){ for(int i=0;i<=n;i++) f[i]=i; } int find(int x){ return x==f[x]?x:f[x]=find(f[x]); } int main(){ #ifdef LMissher freopen("1.in","r",stdin); freopen("1.out","w",stdout); #endif while(~scanf("%d%d",&n,&m)){ init();int cnt=n; for(int i=1;i<=m;i++) scanf("%d%d",&e[i].u,&e[i].v); for(int i=m;i>=1;i--){ ans[i]=cnt; int fx=find(e[i].u),fy=find(e[i].v); if(fx!=fy){ f[fx]=fy; cnt--; } } for(int i=1;i<=m;i++) printf("%d\n",ans[i]); } return 0; }