Advertisement
Josif_tepe

Untitled

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