Advertisement
aayyk

Untitled

Jan 7th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 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.     vector < 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].push_back({ u, v });
  77.             a[y].push_back({ 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.             vector < pii > t;
  87.  
  88.             for (int i = 0; i < sz(a[x]); i++) {
  89.                 auto [n1, n2] = a[x][i];
  90.                 if (find(n2) == y) {
  91.                     ans[n1]++;
  92.                     ans[n2]++;
  93.                 }
  94.                 else {
  95.                     t.push_back(a[x][i]);
  96.  
  97.                 }
  98.             }
  99.  
  100.             for (int i = 0; i < sz(a[y]); i++) {
  101.                 auto [n1, n2] = a[y][i];
  102.                 if (find(n2) != x) {
  103.                     t.push_back(a[y][i]);
  104.                 }
  105.             }
  106.  
  107.  
  108.             if (s[x] < s[y]) {
  109.                 p[x] = y;
  110.                 s[y] += s[x];
  111.  
  112.                
  113.                 a[x].clear();
  114.                 a[y] = t;
  115.             }
  116.             else {
  117.                 p[y] = x;
  118.                 s[x] += s[y];
  119.  
  120.                
  121.                 a[y].clear();
  122.                 a[x] = t;
  123.             }
  124.         }
  125.     }
  126. } a;
  127.  
  128. signed main() {
  129. #ifdef LOCAL
  130.     freopen("input.txt", "r", stdin);
  131.     //freopen("output.txt", "w", stdout);
  132. #endif
  133.     cout << fixed << setprecision(9);
  134.     ios::sync_with_stdio(false);
  135.     cin.tie();
  136.     cout.tie();
  137.  
  138.     int n, m, k;
  139.     cin >> n >> m >> k;
  140.  
  141.     for (int i = 0; i < m; i++) {
  142.         int u, v;
  143.         cin >> u >> v;
  144.  
  145.         a.add_friend(u, v);
  146.     }
  147.  
  148.     for (int i = 0; i < k; ++i) {
  149.         int u, v;
  150.         cin >> u >> v;
  151.  
  152.         a.add_edge(u, v);
  153.     }
  154.  
  155.     int q;
  156.     cin >> q;
  157.  
  158.     while (q--) {
  159.         char c;
  160.         cin >> c;
  161.  
  162.         if (c == '?') {
  163.             int x;
  164.             cin >> x;
  165.  
  166.             cout << a.ans[x] << "\n";
  167.         }
  168.         else {
  169.             int u, v;
  170.             cin >> u >> v;
  171.  
  172.             if (c == 'T') {
  173.                 a.add_edge(u, v);
  174.             }
  175.             else {
  176.                 a.add_friend(u, v);
  177.             }
  178.         }
  179.     }
  180.  
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement