Advertisement
Guest User

Untitled

a guest
May 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <set>
  5. #include <queue>
  6. #include <string>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. struct node {
  12.     int from;
  13.     int to;
  14.     int y;
  15.     node* left;
  16.     node* right;
  17.     node* parent;
  18.     int size;
  19.     node(int f, int t) {
  20.         size = 1;
  21.         left = NULL;
  22.         right = NULL;
  23.         from = f;
  24.         to = t;
  25.         parent = NULL;
  26.         y = rand() * RAND_MAX + rand();
  27.     }
  28. };
  29.  
  30. void print_tree(node* cur) {
  31.     if (!cur) return;
  32.     print_tree(cur->left);
  33.     cout << "(" << cur->from << " -> " << cur->to << ") ";
  34.     print_tree(cur->right);
  35. }
  36.  
  37. int n, m;
  38. vector<node*> nodes(200005);
  39. map<pair<int, int>, node*> edges;
  40.  
  41. int size_of(node* cur) {
  42.     if (cur) return cur->size;
  43.     return 0;
  44. }
  45.  
  46. node* get_root(node* cur) {
  47.     if (cur == NULL) return NULL;
  48.     while (cur->parent != NULL) {
  49.         cur = cur->parent;
  50.     }
  51.     return cur;
  52. }
  53.  
  54. node* get_right(node* cur) {
  55.     if (cur == NULL) return NULL;
  56.     while (cur->right != NULL) {
  57.         cur = cur->right;
  58.     }
  59.     return cur;
  60. }
  61.  
  62. node* get_left(node* cur) {
  63.     if (cur == NULL) return NULL;
  64.     while (cur->left != NULL) {
  65.         cur = cur->left;
  66.     }
  67.     return cur;
  68. }
  69.  
  70. int get_pos(node* cur) {
  71.     if (cur == NULL) return 0;
  72.     int ans = 1;
  73.     if (cur->left != NULL) ans += cur->left->size;
  74.     while (cur->parent != NULL) {
  75.         if (cur == cur->parent->right)
  76.             if (cur->parent->left != NULL) ans += cur->parent->left->size + 1;
  77.             else ans += 1;
  78.         cur = cur->parent;
  79.     }
  80.     return ans;
  81. }
  82.  
  83. void update(node* cur) {
  84.     if (cur == NULL) {
  85.         return;
  86.     }
  87.     cur->size = 1 + size_of(cur->left) + size_of(cur->right);
  88. }
  89.  
  90. pair<node*, node*> split(node* cur, int k) {
  91.     if (cur == NULL) {
  92.         return pair<node*, node*>(NULL, NULL);
  93.     }
  94.     int l = 0;
  95.     if (cur->left != NULL) {
  96.         l = cur->left->size;
  97.     }
  98.     if (k > l) {
  99.         pair<node*, node*> ans = split(cur->right, k - l - 1);
  100.         cur->right = ans.first;
  101.         if (cur->right != NULL)
  102.             cur->right->parent = cur;
  103.         update(cur->right);
  104.         update(cur);
  105.         update(ans.second);
  106.         return make_pair(cur, ans.second);
  107.     }
  108.     else {
  109.         pair<node*, node*> ans = split(cur->left, k);
  110.         cur->left = ans.second;
  111.         if (cur->left != NULL)
  112.             cur->left->parent = cur;
  113.         update(cur->left);
  114.         update(cur);
  115.         update(ans.first);
  116.         return make_pair(ans.first, cur);
  117.     }
  118. }
  119.  
  120. node* merge(node* t1, node* t2) {
  121.     if (t2 == NULL) {
  122.         return t1;
  123.     }
  124.     if (t1 == NULL) {
  125.         return t2;
  126.     }
  127.     if (t1->y > t2->y) {
  128.         t1->right = merge(t1->right, t2);
  129.         if (t1->right != NULL)
  130.             t1->right->parent = t1;
  131.         update(t1->right);
  132.         update(t1);
  133.         return t1;
  134.     }
  135.     else {
  136.         t2->left = merge(t1, t2->left);
  137.         if (t2->left != NULL)
  138.             t2->left->parent = t2;
  139.         update(t2->left);
  140.         update(t2);
  141.         return t2;
  142.     }
  143. }
  144.  
  145. void link(int f, int s) {
  146.     node* u = nodes[f];
  147.     node* v = nodes[s];
  148.     pair<node*, node*> AB = split(get_root(u), get_pos(u));
  149.     pair<node*, node*> CD = split(get_root(v), get_pos(v) );
  150.     node* A = AB.first;
  151.     node* B = AB.second;
  152.     node* C = CD.first;
  153.     node* D = CD.second;
  154.     edges.insert({ { f, s }, new node(f, s) });
  155.     edges.insert({ { s, f }, new node(s, f) });
  156.     if (nodes[f] == nullptr)
  157.         nodes[f] = edges.at({ s, f });
  158.     if (nodes[s] == nullptr)
  159.         nodes[s] = edges.at({ f, s });
  160.     A = merge(A, edges.at({ f, s }));
  161.     A = merge(A, D);
  162.     A = merge(A, C);
  163.     A = merge(A, edges.at({ s, f }));
  164.     A = merge(A, B);
  165.     if (A) A->parent = nullptr;
  166. }
  167.  
  168. void cut(int f, int s) {
  169.     node* u = edges.at({ s, f });
  170.     node* v = edges.at({ f, s });
  171.     int pos_u = get_pos(u);
  172.     int pos_v = get_pos(v);
  173.     if (pos_v < pos_u) {
  174.         swap(f, s);
  175.         swap(pos_u, pos_v);
  176.         swap(u, v);
  177.     }
  178.     pair<node*, node*> ABC = split(get_root(u), pos_u - 1);
  179.     pair<node*, node*> BC = split(ABC.second, pos_v - pos_u);
  180.     node* A = ABC.first;
  181.     node* B = BC.first;
  182.     node* C = BC.second;
  183.     B = split(B, 1).second;
  184.     C = split(C, 1).second;
  185.     edges.erase({ f, s });
  186.     edges.erase({ s, f });
  187.     nodes[f] = get_right(B);
  188.     nodes[s] = get_right(A);
  189.     if (C) nodes[s] = edges.at({ get_left(C)->to, get_left(C)->from });
  190.     node* AC = merge(A, C);
  191.     if (AC) AC->parent = nullptr;
  192.     if (B) B->parent = nullptr;
  193. }
  194.  
  195. bool connected(int f, int s) {
  196.     return get_root(nodes[f]) && get_root(nodes[f]) == get_root(nodes[s]);
  197. }
  198.  
  199. int main() {
  200.     ios_base::sync_with_stdio(false);
  201.     cin.tie(0);
  202.     cout.tie(0);
  203.     cin >> n >> m;
  204.     string q = "";
  205.     for (int i = 0; i < m; i++) {
  206.         int f, s;
  207.         cin >> q;
  208.         if (q == "link") {
  209.             cin >> f >> s;
  210.             link(f, s);
  211.         }
  212.         if (q == "cut") {
  213.             cin >> f >> s;
  214.             cut(f, s);
  215.         }
  216.         if (q == "connected") {
  217.             cin >> f >> s;
  218.             cout << connected(f, s) << '\n';
  219.         }
  220.     }
  221.     return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement