Question: Give You N points and M sides, and ask how many connected parts are there before the I side is deleted.
Train of Thought: from the back-to-front operation, adding an I-I edge from the back-to-back is equal to adding M-I edge and then deleting the front m-I edge. We can see that there is no edge at the beginning, so sum [m] = N;
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 100010#define LL long long using namespace std;int n,m;int father[M];int find(int x) { if(x!=father[x]) return father[x]=find(father[x]); return x; } int sum[M];int main(){ while(~scanf("%d%d",&n,&m)) { int a[M],b[M]; for(int i=0;i<=n;i++) father[i]=i; for(int i=1;i<=m;i++) scanf("%d%d",&a[i],&b[i]); sum[m]=n; for(int i=m;i>0;i--) { if(find(a[i])!=find(b[i])) { sum[i-1]=sum[i]-1; father[find(a[i])]=find(b[i]); } else sum[i-1]=sum[i]; } for(int i=1;i<=m;i++) { printf("%d\n",sum[i]); } } return 0;}