Raslboyy

3558

Nov 21st, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <algorithm>
  5. #include <string>
  6. #include <math.h>
  7. #include <climits>
  8.  
  9. //#include <chrono>
  10. //#include <fstream>
  11. //#include <bits/stdc++.h>
  12.  
  13. #define mp make_pair
  14. #define pb push_back
  15. #define eb emplace_back
  16. #define x first
  17. #define y second
  18. #define sz(x) (int)x.size()
  19. #define all(x) begin(x), end(x)
  20. #define rall(x) rbegin(x), rend(x)
  21. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  22. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  23.  
  24. using namespace std;
  25.  
  26. typedef unsigned long long ull;
  27. typedef long long ll;
  28. typedef long double ld;
  29. typedef pair<int, int> pi;
  30. typedef pair<ll, ll> pl;
  31. typedef vector<int> veci;
  32. typedef vector<bool> vecb;
  33. typedef vector<vector<int>> vvi;
  34. typedef vector<vector<bool>> vvb;
  35.  
  36. const int INF_I = 1e9;
  37. const ll INF_LL = 1e18;
  38.  
  39. int p[100001];
  40. int sz[100001];
  41.  
  42. void RESET(int n) {
  43.     for (int i = 0; i < n; i++) {
  44.         p[i] = i;
  45.         sz[i] = 1;
  46.     }
  47.     cout << "RESET DONE\n";
  48. }
  49.  
  50. int find_set(int v) {
  51.     return (v == p[v] ? v : p[v] = find_set(p[v]));
  52. }
  53.  
  54. void CHECK(int a, int b) {
  55.     cout << (find_set(a) == find_set(b) ? "YES\n" : "NO\n");
  56. }
  57.  
  58. void JOIN(int a, int b) {
  59.     int p_a = find_set(a);
  60.     int p_b = find_set(b);
  61.     if (p_a == p_b) {
  62.         cout << "ALREADY " << a << " " << b << "\n";
  63.         return;
  64.     }
  65.     if (sz[p_a] < sz[p_b])
  66.         swap(p_a, p_b);
  67.     p[p_b] = p_a;
  68.     sz[p_a] += sz[p_b];
  69. }
  70.  
  71. int main() {
  72.     /*ifstream cin("in.txt");
  73.     ofstream cout("out.txt");
  74.     auto start_time = chrono::high_resolution_clock::now();*/
  75.  
  76.     ios::sync_with_stdio(false);
  77.     cin.tie(0);
  78.     cout.tie(0);
  79.  
  80.     string str;
  81.     int a, b, n;
  82.     while (cin >> str) {
  83.         switch (str[0])
  84.         {
  85.         case 'J':
  86.             cin >> a >> b;
  87.             JOIN(a, b);
  88.             break;
  89.         case 'C':
  90.             cin >> a >> b;
  91.             CHECK(a, b);
  92.             break;
  93.         case 'R':
  94.             cin >> n;
  95.             RESET(n);
  96.             break;
  97.         }
  98.     }
  99.    
  100.  
  101.    
  102.     /*auto end_time = chrono::high_resolution_clock::now();
  103.     chrono::duration<double> duration = end_time - start_time;
  104.     cout << duration.count() << "\n";
  105.     cin.close();
  106.     cout.close();*/
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment