aayyk

Untitled

Jan 7th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 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. #define mp make_pair
  38. #define pb push_back
  39. #define sz(x) int((x).size())
  40.  
  41. #ifndef LOCAL
  42. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  43. #else
  44. mt19937 rng(228);
  45. #endif
  46.  
  47. const int N = 1e5 + 7;
  48. const int inf = INT_MAX / 2;
  49. const ll INF = LLONG_MAX / 3;
  50. const int MOD = 1e9 + 7;
  51. const double eps = 1e-6;
  52. const string cars[] = {"🚗", "🚕", "🚙"};
  53.  
  54. struct dsu {
  55.     int p[N], s[N], ans[N];
  56.     set < pii > a[N];
  57.  
  58.     dsu() {
  59.         iota(p, p + N, 0);
  60.         memset(s, 1, sizeof s);
  61.     }
  62.  
  63.     int find(int u) {
  64.         return (p[u] == u ? u : p[u] = find(p[u]));
  65.     }
  66.  
  67.     void add_friend(int u, int v) {
  68.         if (find(u) == find(v)) {
  69.             ans[u]++;
  70.             ans[v]++;
  71.         }
  72.         else {
  73.             int x = find(u);
  74.             int y = find(v);
  75.  
  76.             a[x].insert({ u, v });
  77.             a[y].insert({ v, u });
  78.         }
  79.     }
  80.  
  81.     void add_edge(int u, int v) {
  82.         int x = find(u);
  83.         int y = find(v);
  84.  
  85.         if (x != y) {
  86.             if (s[x] < s[y]) {
  87.                 p[x] = y;
  88.                 s[y] += s[x];
  89.  
  90.                 for (auto it = a[x].begin(); it != a[x].end(); ) {
  91.                     auto [n1, n2] = *it;
  92.                     if (find(n2) == y) {
  93.                         ans[n1]++;
  94.                         ans[n2]++;
  95.  
  96.                         a[y].erase({ n2, n1 });
  97.  
  98.                         auto ptr = ++it;
  99.                         a[x].erase(--it);
  100.                         it = ptr;
  101.                     }
  102.                     else {
  103.                         a[y].insert(*it);
  104.                         it++;
  105.                     }
  106.                 }
  107.             }
  108.             else {
  109.                 p[y] = x;
  110.                 s[x] += s[y];
  111.  
  112.                 for (auto it = a[y].begin(); it != a[y].end(); ) {
  113.                     auto [n1, n2] = *it;
  114.                     if (find(n2) == x) {
  115.                         ans[n1]++;
  116.                         ans[n2]++;
  117.  
  118.                         a[x].erase({ n2, n1 });
  119.  
  120.                         auto ptr = ++it;
  121.                         a[y].erase(--it);
  122.                         it = ptr;
  123.                     }
  124.                     else {
  125.                         a[x].insert(*it);
  126.                         it++;
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.     }
  132. } a;
  133.  
  134. signed main() {
  135. #ifdef LOCAL
  136.     freopen("input.txt", "r", stdin);
  137.     //freopen("output.txt", "w", stdout);
  138. #endif
  139.     cout << fixed << setprecision(9);
  140.     ios::sync_with_stdio(false);
  141.     cin.tie();
  142.     cout.tie();
  143.  
  144.     int n, m, k;
  145.     cin >> n >> m >> k;
  146.  
  147.     for (int i = 0; i < m; i++) {
  148.         int u, v;
  149.         cin >> u >> v;
  150.  
  151.         a.add_friend(u, v);
  152.     }
  153.  
  154.     for (int i = 0; i < k; ++i) {
  155.         int u, v;
  156.         cin >> u >> v;
  157.  
  158.         a.add_edge(u, v);
  159.     }
  160.  
  161.     int q;
  162.     cin >> q;
  163.  
  164.     while (q--) {
  165.         char c;
  166.         cin >> c;
  167.  
  168.         if (c == '?') {
  169.             int x;
  170.             cin >> x;
  171.  
  172.             cout << a.ans[x] << "\n";
  173.         }
  174.         else {
  175.             int u, v;
  176.             cin >> u >> v;
  177.  
  178.             if (c == 'T') {
  179.                 a.add_edge(u, v);
  180.             }
  181.             else {
  182.                 a.add_friend(u, v);
  183.             }
  184.         }
  185.     }
  186.  
  187.  
  188.     return 0;
  189. }
Add Comment
Please, Sign In to add comment