Advertisement
Josif_tepe

Untitled

Feb 18th, 2024
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int maxn = 100005;
  4. int idx[maxn];
  5. int sz[maxn];
  6. int n;
  7. void init() {
  8.     for(int i = 0; i < maxn; i++) {
  9.         idx[i] = i;
  10.         sz[i] = 1;
  11.     }
  12. }
  13. int root(int x) {
  14.     while(idx[x] != x) {
  15.         idx[x] = idx[idx[x]];
  16.         x = idx[x];
  17.     }
  18.     return x;
  19. }
  20. void join(int A, int B) {
  21.     int root_A = root(A);
  22.     int root_B = root(B);
  23.     if(root_A != root_B) {
  24.         if(sz[root_A] < sz[root_B]) {
  25.             idx[root_A] = idx[root_B];
  26.             sz[root_B] += sz[root_A];
  27.         }
  28.         else {
  29.             idx[root_B] = idx[root_A];
  30.             sz[root_A] += sz[root_B];
  31.         }
  32.     }
  33. }
  34. bool check(int A, int B) {
  35.     return root(A) == root(B);
  36. }
  37. int main()
  38. {
  39.     cin >> n;
  40.     init();
  41.     vector<int> v(n);
  42.     for(int i = 0; i < n; i++) {
  43.         cin >> v[i];
  44.     }
  45.    
  46.     for(int i = 0; i < n; i++) {
  47.         for(int j = 0; j < n; j++) {
  48.             if(v[i] == v[j] - 1 or v[i] == v[j] + 1) {
  49.                 join(i, j);
  50.             }
  51.         }
  52.     }
  53.     int res = 0;
  54.     for(int i = 0; i < n; i++) {
  55.         res = max(res, sz[i]);
  56.     }
  57.     cout << res << endl;
  58.    
  59.     return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement