Advertisement
Josif_tepe

Untitled

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