Advertisement
Josif_tepe

Untitled

Dec 19th, 2022
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. const int maxn = 105;
  5. int idx[maxn];
  6. int find_root(int x) {
  7.     while(x != idx[x]) {
  8.         idx[x] = idx[idx[x]];
  9.         x = idx[x];
  10.     }
  11.     return x;
  12. }
  13. bool check(int a, int b) {
  14.     if(find_root(a) == find_root(b)) {
  15.         return true;
  16.     }
  17.     return false;
  18. }
  19. void unite_two_elements(int a, int b) {
  20.     int root_a = find_root(a);
  21.     int root_b = find_root(b);
  22.     idx[root_a] = root_b;
  23. }
  24. int main() {
  25.     for(int i = 0; i < maxn; i++) {
  26.         idx[i] = i;
  27.     }
  28.    
  29.     while(true){
  30.         string s;
  31.         int a, b;
  32.         cin >> s >> a >> b;
  33.        
  34.         if(s == "unite") {
  35.             unite_two_elements(a, b);
  36.         }
  37.         if(s == "check") {
  38.             if(check(a, b)) {
  39.                 cout << "YES" << endl;
  40.             }
  41.             else {
  42.                 cout << "NO" << endl;
  43.             }
  44.         }
  45.     }
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement