Josif_tepe

Untitled

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