Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // second
- using namespace std;
- template<typename T>
- struct graph {
- string vertex(string name, long long int index) {
- Node* n = nodes[name];
- if (index < 0 || index >= n->vc.size())
- return "";
- return n->vc[index].second->name;
- }
- string first(string name) {
- return vertex(std::move(name), 0);
- }
- string next(string name, long long int index) {
- return vertex(std::move(name), index + 1);
- }
- void add_v(string name, T mark) {
- nodes[name] = new Node(name, mark);
- }
- void add_e(string first, string second, long long int weight, bool is_oriented=true) {
- Node* f = nodes[first];
- Node* s = nodes[second];
- f->vc.push_back({weight, s});
- if (!is_oriented)
- s->vc.push_back({weight, f});
- }
- void del_v(string name) {
- Node* n = nodes[name];
- for (auto& x : nodes) {
- if (n != x.second) {
- std::remove_if(x.second->vc.begin(), x.second->vc.end(), [](auto& y){return y.second == n;});
- }
- }
- nodes.erase(n);
- }
- void del_e(string first, string second, bool is_oriented=true) {
- Node* f = nodes[first];
- Node* s = nodes[second];
- std::remove_if(f->vc.begin(), f->vc.end(), [](auto& y){return y.second == s;});
- if (!is_oriented)
- std::remove_if(s->vc.begin(), s->vc.end(), [](auto& y){return y.second == f;});
- }
- void edit_v(string name, T mark) {
- nodes[name]->mark = mark;
- }
- void edit_e(string first, string second, long long int weight, bool is_oriented) {
- Node* f = nodes[first];
- Node* s = nodes[second];
- replace(f->vc.begin(), f->vc.end(), [](auto& x){return x.second == s;}, {weight, s});
- if (!is_oriented)
- replace(s->vc.begin(), s->vc.end(), [](auto& x){return x.second == s;}, {weight, f});
- }
- private:
- struct Node{
- Node(string name, T mark) : name(name), mark(mark) {}
- vector<pair<long long int, Node*> > vc;
- string name;
- T mark;
- };
- unordered_map<string, Node*> nodes;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement