貌似某大犇说过 正难则反,,,
题目说要对这张图进行删边,然后判断联通块的个数,那么就可以先把所有边都删掉,之后从后往前加边,若加的边两端点不在同一个联通块中,
那么此时联通快个数少一,否则不变
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 const int maxn = 10000 + 500; 6 const int maxm = 100000 + 5000; 7 int father[maxn]; 8 int x1[maxm], x2[maxm]; 9 int ans[maxm]; 10 int n, m; 11 12 13 int getfather(int x) { 14 if (father[x] == x) return (x); 15 return (father[x] = getfather(father[x])); 16 } 17 18 int main () { 19 while(scanf("%d %d", &n, &m) != EOF) { 20 memset(ans, 0, sizeof(ans)); 21 for (int i = 1; i <= n; i++) father[i] = i; 22 for (int i = 1; i <= m; i++) { 23 scanf("%d %d", &x1[i], &x2[i]); 24 x1[i] += 1; 25 x2[i] += 1; 26 } 27 ans[m] = n; 28 for (int i = m; i >= 1; i--) { 29 int tx = getfather(x1[i]); 30 int ty = getfather(x2[i]); 31 if (tx != ty) { 32 ans[i-1] = ans[i] - 1; 33 father[tx] = ty; 34 } else { 35 ans[i-1] = ans[i]; 36 } 37 } 38 for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); 39 } 40 return 0; 41 }