Advertisement
Josif_tepe

Untitled

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