Advertisement
Josif_tepe

Untitled

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