1-ShadowMaster-1

Untitled

Apr 17th, 2022
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #pragma GCC optimize("Ofast")
  3.  
  4. #define fi first
  5. #define se second
  6. #define ll long long
  7. #define dl double
  8.  
  9. using namespace std;
  10.  
  11. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  12.  
  13. template<class K, class V>
  14. class node{
  15. public:
  16.         K key;
  17.         V val = 0;
  18.         int degree = -1;
  19.         node<K, V>* child = nullptr;
  20.         bool mark = false;
  21.         node<K, V>* p = nullptr;
  22.         node<K, V>* left = nullptr;
  23.         node<K, V>* right = nullptr;
  24. };
  25.  
  26. template<class K, class V>
  27. class PriorityQueue{
  28.  
  29. public:
  30.         int length = 0;
  31.         node<K, V>* min = nullptr;
  32.         node<K, V>* left = nullptr;
  33.         node<K, V>* right = nullptr;
  34.  
  35.  
  36.         void insert(K key, V value)
  37.         {
  38.                 length++;
  39.                 auto nEl = new node<K, V>;
  40.                 nEl->val = value;
  41.                 nEl->key = key;
  42.                 nEl->left = nEl;
  43.                 nEl->right = nEl;
  44.                 nEl->degree = 0;
  45.                 PriorityQueue<K, V> nW;
  46.                 nW.min = nEl;
  47.                 nW.left = nEl;
  48.                 nW.right = nEl;
  49.                 unio(nW);
  50.         }
  51.  
  52.         node<K, V> findMin()
  53.         {
  54.                 return *min;
  55.         }
  56.  
  57.         node<K, V> extractMin()
  58.         {
  59.                 length--;
  60.                 node<K, V> res = *min;
  61.                 if(min->left == min && min->degree == 0){
  62.                         min = nullptr;
  63.                         left = nullptr;
  64.                         right = nullptr;
  65.                         return res;
  66.                 }
  67.                 auto L = min->left;
  68.                 auto R = min->right;
  69.                 auto y = min->child;
  70.                 if(min->left == min){
  71.                         min = nullptr;
  72.                         left = nullptr;
  73.                         right = nullptr;
  74.                 }else{
  75.                         if(length == 1){
  76.                                 left = min->left;
  77.                                 right = min->left;
  78.                                 right->left = right;
  79.                                 right->right = right;
  80.                                 left = right;
  81.                                 min = right;
  82.                         }else{
  83.                                 L->right = R;
  84.                                 R->left = L;
  85.                                 left = R;
  86.                                 right = L;
  87.                                 min = left;
  88.                         }
  89.                 }
  90.                 vector<node<K, V>*> v;
  91.                 for(int i = 0; i < res.degree; i++){
  92.                         v.push_back(y);
  93.                         y = y->right;
  94.                         length++;
  95.                 }
  96.                 for(auto &x : v){
  97.                         delete x->p;
  98.                         x->p = nullptr;
  99.                         x->left = x;
  100.                         x->right = x;
  101.                         PriorityQueue<K, V> nW;
  102.                         nW.min = x;
  103.                         nW.left = x;
  104.                         nW.right = x;
  105.                         unio(nW);
  106.                 }
  107.                 consilidate();
  108.                 return res;
  109.         }
  110.  
  111.         void consilidate()
  112.         {
  113.                 assert(min != nullptr);
  114.                 int l = 30;
  115.                 vector<node<K, V>*> d(l, nullptr);
  116.                 auto x = min;
  117.                 int cnt = length;
  118.                 for(int i = 0; i < cnt; i++){
  119.                         while(d[x->degree] != nullptr && d[x->degree] != x){
  120.                                 auto F = x;
  121.                                 auto S = d[x->degree];
  122.                                 if(F->val > S->val || (F->val == S->val && F->key > S->key)){
  123.                                         swap(F, S);
  124.                                 }
  125.                                 length--;
  126.                                 auto L = S->left;
  127.                                 auto R = S->right;
  128.                                 assert(L != S && R != S);
  129.                                 L->right = R;
  130.                                 R->left = L;
  131.                                 S->p = F;
  132.                                 if(F->child == nullptr){
  133.                                         S->left = S;
  134.                                         S->right = S;
  135.                                         F->child = S;
  136.                                 }else{
  137.                                         if(F->degree == 1){
  138.                                                 S->left = F->child;
  139.                                                 S->right = F->child;
  140.                                                 F->child->left = S;
  141.                                                 F->child->right = S;
  142.                                                 F->child = S;
  143.                                         }else{
  144.                                                 S->left = F->child;
  145.                                                 S->right = F->child->right;
  146.                                                 F->child->right->left = S;
  147.                                                 F->child->right = S;
  148.                                                 F->child = S;
  149.                                         }
  150.                                 }
  151.                                 d[x->degree] = nullptr;
  152.                                 F->degree += 1;
  153.                                 left = F;
  154.                                 right = F->left;
  155.                                 min = F;
  156.                                 x = F;
  157.                         }
  158.                         d[x->degree] = x;
  159.                         x = x->right;
  160.                 }
  161.                 x = left;
  162.                 for(int i = 0; i < length; i++){
  163.                         if(i == 0 || ((min->val > x->val) || (min->val == x->val && min->key > x->key))){
  164.                                 min = x;
  165.                         }
  166.                         x = x->right;
  167.                 }
  168.         }
  169.  
  170.         void unio(PriorityQueue<K,V> anotherQueue)
  171.         {
  172.                 if(min == nullptr || (anotherQueue.min->val < min->val ||
  173.                                       (anotherQueue.min->val == min->val && anotherQueue.min->key < min->key))){
  174.                         min = anotherQueue.min;
  175.                 }
  176.                 if(right == nullptr){
  177.                         left = anotherQueue.left;
  178.                         right = anotherQueue.right;
  179.                         return;
  180.                 }
  181.                 if(left == right){
  182.                         anotherQueue.left->left = right;
  183.                         anotherQueue.right->right = left;
  184.                         left->right = anotherQueue.left;
  185.                         left->left = anotherQueue.right;
  186.                         right = anotherQueue.right;
  187.                         return;
  188.                 }
  189.                 anotherQueue.left->left = right;
  190.                 anotherQueue.right->right = left;
  191.                 assert(left->left == right);
  192.                 right->right = anotherQueue.left;
  193.                 anotherQueue.left->left = right;
  194.                 left->left = anotherQueue.right;
  195.                 anotherQueue.right->right = left;
  196.                 right = anotherQueue.right;
  197.  
  198.         }
  199.  
  200.         bool empty()
  201.         {
  202.                 return (length == 0);
  203.         }
  204.  
  205. };
  206.  
  207. template<class V, class E>
  208. class Vertex{
  209. public:
  210.         int id;
  211.         E penalty;
  212. };
  213.  
  214. template<class V, class E>
  215. class Edge{
  216. public:
  217.         Vertex<V, E> from;
  218.         Vertex<V, E> to;
  219.         E w;
  220. };
  221.  
  222. template<class V, class E>
  223. class IGraph {
  224.         virtual Vertex<V, E> insertVertex(V& v, E& w) = 0;
  225.         virtual void insertEdge(Vertex<V, E>& from, Vertex<V, E>& to, E& w) = 0;
  226.         //virtual void removeVertex(Vertex<V, E> v) = 0;
  227.         //virtual void removeEdge(Edge<V, E> e) = 0;
  228.         //virtual bool areAdjacent(Vertex<V, E> v, Vertex<V, E> u) = 0;
  229.         virtual int degree(Vertex<V, E> &v) = 0;
  230. };
  231.  
  232. template<class V, class E>
  233. class DynamicGraph : public IGraph<V, E> {
  234. public:
  235.         int N;
  236.         vector<vector<E>> ed;
  237.         vector<vector<pair<E,int>>> v;
  238.         vector<int> degre,del;
  239.         vector<V> g;
  240.         map<V, int> m;
  241.         int G = 0;
  242.  
  243.         DynamicGraph()
  244.         {
  245.                 N = 0;
  246.                 int F = 3500;
  247.                 degre.resize(F);
  248.                 del.resize(F);
  249.                 ed.resize(F);
  250.                 g.resize(F);
  251.                // v.resize(F);
  252.                 for(int i = 0; i < F; i++){
  253.                         ed[i].resize(F);
  254.                 }
  255.                 for(int i = 0; i < F; i++){
  256.                         for(int j = 0; j < F; j++){
  257.                                 ed[i][j] = -1;
  258.                         }
  259.                 }
  260.         }
  261.  
  262.         int degree(Vertex<V, E>& v)override
  263.         {
  264.                 return degre[v.id];
  265.         }
  266.  
  267.         Vertex<V, E> insertVertex(V& v, E& w)override
  268.         {
  269.                 Vertex<V, E> nv;
  270.                 if(m.find(v) == m.end()){
  271.                         m[v] = G++;
  272.                 }
  273.                 if(m[v] == static_cast<int>(degre.size())){
  274.                         int len = static_cast<int>(degre.size()) + 1;
  275.                         degre.push_back(0);
  276.                         g.push_back("");
  277.                         E x;
  278.                         x = -1;
  279.                         //this->v.push_back({});
  280.                         for(int i = 0; i < len - 1; i++){
  281.                                 ed[i].push_back(x);
  282.                         }
  283.                         ed.push_back(vector<E> (len, -1));
  284.                 }
  285.                 N++;
  286.                 nv.id = m[v];
  287.                 nv.penalty = w;
  288.                 g[nv.id] = v;
  289.                 return nv;
  290.         }
  291.  
  292.         void insertEdge(Vertex<V, E>& from, Vertex<V, E>& to, E& w)override
  293.         {
  294.                 E W = w / (from.penalty + to.penalty);
  295.                 //v[from.id].push_back({w, to.id});
  296.                 //v[to.id].push_back({w, from.id});
  297.                 degre[from.id]++;
  298.                 degre[to.id]++;
  299.                 ed[from.id][to.id] = W;
  300.                 ed[to.id][from.id] = W;
  301.                 //return nw;
  302.         }
  303.  
  304.         /*bool areAdjacent(Vertex<V, E> v, Vertex<V, E> u)override
  305.         {
  306.                 return (ed[v.id][u.id] != -1);
  307.         }
  308.  
  309.         void removeVertex(Vertex<V, E> v)override
  310.         {
  311.                 del[v.id] = 1;
  312.                 for(int i = 0; i < N; i++){
  313.                         if(!del[i] && i != v.id && ed[i][v.id] != -1){
  314.                                 removeEdge(ed[i][v.id]);
  315.                         }
  316.                 }
  317.         }
  318.  
  319.         void removeEdge(Edge<V, E> e)override
  320.         {
  321.                 ed[e.from.id][e.to.id].w = -1;
  322.                 ed[e.from.id][e.to.id].w = -1;
  323.         }*/
  324. };
  325.  
  326. template<class V, class E>
  327. class Prim{
  328. public:
  329.         DynamicGraph<V, E> g;
  330.         void mst()
  331.         {
  332.                 vector<E> D(g.N, 1e9);
  333.                 //for(int i = 0; i < g.N; i++){
  334.                 //        D[i] = 1e9;
  335.                 //}
  336.                 vector<int> par(g.N, -1);
  337.                 vector<bool> vis(g.N, false);
  338.                 for(int i = 0; i < g.N; i++){
  339.                         if(vis[i])continue;
  340.                         priority_queue<pair<E, int>> q;
  341.                         D[i] = 0;
  342.                         vis[i] = true;
  343.                         q.push(make_pair(0, i));
  344.                         while(!q.empty()){
  345.                                 auto w = -q.top().first;
  346.                                 auto v = q.top().second;
  347.                                 q.pop();
  348.                                 if(D[v] < w)continue;
  349.                                 if(par[v] != -1){
  350.                                         cout << g.g[par[v]] << ":" << g.g[v] << " ";
  351.                                         vis[v] = true;
  352.                                 }
  353.                                 int cur = 0;
  354.                                 for(int j = 0; j < g.N && cur < g.degre[v]; j++){
  355.                                         if(vis[j])continue;
  356.                                         if(g.ed[v][j] != -1){
  357.                                                 cur++;
  358.                                                 if(D[j] > g.ed[v][j]){
  359.                                                         D[j] = g.ed[v][j];
  360.                                                         q.push({-D[j], j});
  361.                                                         par[j] = v;
  362.                                                 }
  363.                                         }
  364.                                 }
  365.                         }
  366.                 }
  367.                 cout << "\n";
  368.         }
  369. };
  370.  
  371. int main()
  372. {
  373.         ios_base::sync_with_stdio(false);
  374.         cin.tie(NULL);
  375.         //freopen( "input.txt" , "r" , stdin );
  376.         //freopen( "output.txt" , "w" , stdout );
  377.  
  378.         int n;
  379.         cin >> n;
  380.         Prim<string, float> v;
  381.         map<string, Vertex<string, float>> m;
  382.         for(int i = 1; i <= n; i++){
  383.                 string s;
  384.                 cin >> s;
  385.                 if(s == "ADD"){
  386.                         string ve;
  387.                         float penalty;
  388.                         cin >> ve >> penalty;
  389.                         Vertex<string, float> n_x = v.g.insertVertex(ve, penalty);
  390.                         m[ve] = n_x;
  391.                 }else if(s == "CONNECT"){
  392.                         string V,U;
  393.                         float w;
  394.                         cin >> V >> U >> w;
  395.                         v.g.insertEdge(m[V], m[U], w);
  396.                 }else{
  397.                         v.mst();
  398.                 }
  399.         }
  400. }
  401.  
  402.  
Advertisement
Add Comment
Please, Sign In to add comment