貌似某大犇说过 正难则反,,,
题目说要对这张图进行删边,然后判断联通块的个数,那么就可以先把所有边都删掉,之后从后往前加边,若加的边两端点不在同一个联通块中,
那么此时联通快个数少一,否则不变
#include <cstdio>
#include <cstring>
#include <algorithm> const int maxn = + ;
const int maxm = + ;
int father[maxn];
int x1[maxm], x2[maxm];
int ans[maxm];
int n, m; int getfather(int x) {
if (father[x] == x) return (x);
return (father[x] = getfather(father[x]));
} int main () {
while(scanf("%d %d", &n, &m) != EOF) {
memset(ans, , sizeof(ans));
for (int i = ; i <= n; i++) father[i] = i;
for (int i = ; i <= m; i++) {
scanf("%d %d", &x1[i], &x2[i]);
x1[i] += ;
x2[i] += ;
}
ans[m] = n;
for (int i = m; i >= ; i--) {
int tx = getfather(x1[i]);
int ty = getfather(x2[i]);
if (tx != ty) {
ans[i-] = ans[i] - ;
father[tx] = ty;
} else {
ans[i-] = ans[i];
}
}
for (int i = ; i <= m; i++) printf("%d\n", ans[i]);
}
return ;
}