Advertisement
Josif_tepe

Untitled

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