Advertisement
CyberN00b

a_graph

Dec 26th, 2022 (edited)
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // second
  3. using namespace std;
  4.  
  5. template<typename T>
  6. struct graph {
  7.  
  8.     string vertex(string name, long long int index) {
  9.         Node* n = nodes[name];
  10.         if (index < 0 || index >= n->vc.size())
  11.             return "";
  12.         return n->vc[index].second->name;
  13.     }
  14.  
  15.     string first(string name) {
  16.         return vertex(std::move(name), 0);
  17.     }
  18.  
  19.     string next(string name, long long int index) {
  20.         return vertex(std::move(name), index + 1);
  21.     }
  22.  
  23.     void add_v(string name, T mark) {
  24.         nodes[name] = new Node(name, mark);
  25.     }
  26.  
  27.     void add_e(string first, string second, long long int weight, bool is_oriented=true) {
  28.         Node* f = nodes[first];
  29.         Node* s = nodes[second];
  30.         f->vc.push_back({weight, s});
  31.         if (!is_oriented)
  32.             s->vc.push_back({weight, f});
  33.     }
  34.  
  35.     void del_v(string name) {
  36.         Node* n = nodes[name];
  37.         for (auto& x : nodes) {
  38.             if (n != x.second) {
  39.                 std::remove_if(x.second->vc.begin(), x.second->vc.end(), [](auto& y){return y.second == n;});
  40.             }
  41.         }
  42.         nodes.erase(n);
  43.     }
  44.  
  45.     void del_e(string first, string second, bool is_oriented=true) {
  46.         Node* f = nodes[first];
  47.         Node* s = nodes[second];
  48.         std::remove_if(f->vc.begin(), f->vc.end(), [](auto& y){return y.second == s;});
  49.         if (!is_oriented)
  50.             std::remove_if(s->vc.begin(), s->vc.end(), [](auto& y){return y.second == f;});
  51.     }
  52.  
  53.     void edit_v(string name, T mark) {
  54.         nodes[name]->mark = mark;
  55.     }
  56.  
  57.     void edit_e(string first, string second, long long int weight, bool is_oriented) {
  58.         Node* f = nodes[first];
  59.         Node* s = nodes[second];
  60.         replace(f->vc.begin(), f->vc.end(), [](auto& x){return x.second == s;}, {weight, s});
  61.         if (!is_oriented)
  62.             replace(s->vc.begin(), s->vc.end(), [](auto& x){return x.second == s;}, {weight, f});
  63.     }
  64.  
  65. private:
  66.  
  67.     struct Node{
  68.         Node(string name, T mark) : name(name), mark(mark) {}
  69.  
  70.         vector<pair<long long int, Node*> > vc;
  71.  
  72.         string name;
  73.  
  74.         T mark;
  75.     };
  76.  
  77.     unordered_map<string, Node*> nodes;
  78.  
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement