fork download
  1. #include <iostream>
  2. using namespace std;
  3. int p[1000005];
  4. int n, m, mx, a, b;
  5.  
  6. int find_root(int x){
  7. if (p[x] < 0) return x;
  8. else {
  9. p[x] = find_root(p[x]);
  10. return p[x];
  11. }
  12. }
  13.  
  14. int main() {
  15. while (cin >> n >> m){
  16. for (int i = 0; i < n; i++){
  17. p[i] = -1; // 初始化
  18. }
  19. mx = 1;
  20. for (int i = 0; i < m; i++){
  21. cin >> a >> b;
  22. a = find_root(a);
  23. b = find_root(b);
  24. if (a != b){ // 不同才能加
  25. p[a] += p[b]; // 加總
  26. mx = max(mx, -p[a]); // 順道紀錄最大值
  27. p[b] = a; // 合併
  28. }
  29. }
  30. cout << mx << "\n";
  31. }
  32. }
Success #stdin #stdout 0.01s 7456KB
stdin
6 4
0 1
2 3
1 3
5 4
1000000 0
1000000 1
0 999999
stdout
4
1
2