Advertisement
Josif_tepe

Untitled

Mar 7th, 2025
160
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. using namespace std;
  3. const int maxn = 1e5 + 10;
  4. int idx[maxn];
  5.  
  6. void init() {
  7.     for(int i = 0; i < maxn; i++) {
  8.         idx[i] = i;
  9.     }
  10. }
  11.  
  12. void unite(int A, int B) {
  13.     int root_A = idx[A];
  14.     for(int i = 0; i < maxn; i++) {
  15.         if(idx[i] == root_A) {
  16.             idx[i] = idx[B];
  17.         }
  18.     }
  19.    
  20. }
  21. bool check_if_they_belong_to_same_set(int A, int B) {
  22.     return idx[A] == idx[B];
  23.    
  24. }
  25. int main()
  26. {
  27.     init();
  28.     while(true) {
  29.         string s;
  30.         cin >> s;
  31.         if(s == "end") {
  32.             break;
  33.         }
  34.         if(s == "union") {
  35.             int a, b;
  36.             cin >> a >> b;
  37.             unite(a, b);
  38.         }
  39.         else {
  40.             int a, b;
  41.             cin >> a >> b;
  42.             cout << check_if_they_belong_to_same_set(a, b) << endl;
  43.         }
  44.     }
  45.  
  46.    
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement