Advertisement
Josif_tepe

Untitled

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