Josif_tepe

Untitled

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