标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2649 Accepted Submission(s): 922
#include <cstdio> #include <cstring> #include <iostream> #define N 10001 #define M 100001 using namespace std; int n, m; int a[M], b[M], num[M], father[N]; void Init() { num[m-1] = n; for(int i = 0; i < n; i++) father[i] = i; } int Find(int a) { if(a == father[a]) return a; else return father[a] = Find(father[a]); } void Mercy(int a, int b, int c) { int Q = Find(a); int P = Find(b); if(Q != P) { father[Q] = P; num[c-1] = num[c] - 1; } else num[c-1] = num[c]; } int main() { while(~scanf("%d%d", &n, &m)) { Init(); for(int i = 0; i < m; i++) scanf("%d%d", &a[i], &b[i]); for(int i = m-1; i >= 0; i--) //反向 ; Mercy(a[i], b[i], i); for(int i = 0; i < m; i++) printf("%d\n", num[i]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4857289.html