Advertisement
aayyk

Untitled

Mar 28th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <climits>
  9. #include <string>
  10. #include <set>
  11. #include <cmath>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <numeric>
  15. #include <random>
  16. #include <memory>
  17. #include <chrono>
  18. #include <iterator>
  19. #include <functional>
  20. #include <unordered_set>
  21. #include <cassert>
  22. #include <cstring>
  23. #ifdef LOCAL
  24. #include "debug.h"
  25. #else
  26. #define debug(x...)
  27. #endif
  28. //#pragma GCC optimize("Ofast")
  29. //#define int ll
  30.  
  31.  
  32.  
  33. using namespace std;
  34. typedef long long ll;
  35. typedef long double ld;
  36. typedef pair < int, int > pii;
  37. typedef pair < ll, ll > pll;
  38. #define sz(x) int((x).size())
  39.  
  40. #ifndef LOCAL
  41. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  42. #else
  43. mt19937 rng(228);
  44. #endif
  45.  
  46. const int N = 2e5 + 7;
  47. const int inf = INT_MAX / 2;
  48. const ll INF = LLONG_MAX / 3;
  49. const int MOD = 1e9 + 7;
  50. const double eps = 1e-6;
  51. const string cars[] = {"🚗", "🚕", "🚙"};
  52.  
  53.  
  54. struct DSU {
  55.     int p[N], rank[N];
  56.     char c[N];
  57.     ll s[N], cnt, n;
  58.  
  59.     DSU(int _n) {
  60.         cnt = n = _n;
  61.         for (int i = 1; i <= n; i++) {
  62.             p[i] = i;
  63.             s[i] = 1;
  64.         }
  65.     }
  66.  
  67.     inline int find(int u) {
  68.         return (p[u] == u ? u : p[u] = find(p[u]));
  69.     }
  70.  
  71.     bool join(int u, int v) {
  72.         u = find(u);
  73.         v = find(v);
  74.  
  75.         if (u == v) {
  76.             if (c[u]) {
  77.                 return false;  
  78.             }
  79.  
  80.             cnt -= s[u];
  81.             c[u] = 1;
  82.             return true;
  83.         }
  84.         else {
  85.             char flag = (c[u] | c[v]);
  86.             bool res = !(c[u] && c[v]);
  87.  
  88.             if (res) {
  89.                 if (flag) {
  90.                     if (!c[u]) {
  91.                         cnt -= s[u];
  92.                     }
  93.                     else {
  94.                         cnt -= s[v];
  95.                     }
  96.                 }
  97.  
  98.                 if (rank[u] < rank[v]) {
  99.                     swap (u, v);
  100.                 }
  101.            
  102.                 p[v] = u;
  103.                 s[u] += s[v];
  104.                 c[u] = flag;
  105.                 rank[u] += (rank[u] == rank[v]);
  106.                
  107.                 return true;
  108.             }
  109.             return false;
  110.         }
  111.     }
  112.  
  113.     inline const bool ask(int u, int v) {
  114.         return !(c[find(u)] && c[find(v)]);
  115.        
  116.     }
  117.  
  118.     inline const ll count() const {
  119.         return cnt * (cnt - 1) + 2 * cnt * (n - cnt);
  120.     }
  121. };
  122.  
  123. signed main() {
  124. #ifdef LOCAL
  125.     freopen("input.txt", "r", stdin);
  126.     //freopen("output.txt", "w", stdout);
  127. #endif
  128.     cout << fixed << setprecision(4);
  129.     ios::sync_with_stdio(false), cin.tie(), cout.tie();
  130.  
  131.     int n, m, q;
  132.     cin >> n >> m >> q;
  133.  
  134.     DSU dsu = DSU(n);
  135.  
  136.     while (m--) {
  137.         int x, y;
  138.         cin >> x >> y;
  139.  
  140.         //debug(x, y);
  141.  
  142.         dsu.join(x, y);
  143.     }
  144.  
  145.     while (q--) {
  146.         short type;
  147.         cin >> type;
  148.  
  149.         //debug(type);
  150.  
  151.         if (type == 1) {
  152.             int x, y;
  153.             cin >> x >> y;
  154.  
  155.             cout << (dsu.ask(x, y) ? "Yes\n" : "No\n");
  156.         }
  157.         else if (type == 2) {
  158.             int x, y;
  159.             cin >> x >> y;
  160.  
  161.             cout << (dsu.join(x, y) ? "Yes\n" : "No\n");
  162.         }
  163.         else {
  164.             cout << dsu.count() << "\n";
  165.         }
  166.     }
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement