Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <vector>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. void dfsVisit(const vector<vector<int>> &g, vector<bool> &used, const int &v, stack<int> &nodes_stack){
  8.     used[v] = true;
  9.     for (const int &u : g[v]){
  10.         if (!used[u]){
  11.             dfsVisit(g, used, u, nodes_stack);
  12.         }
  13.     }
  14.     nodes_stack.push(v);
  15. }
  16.  
  17. void getNodesStack(const vector<vector<int>> &g, stack<int> &nodes_stack){
  18.     vector<bool> used(g.size(), false);
  19.     for (int v = 0; v < g.size(); ++v){
  20.         if (!used[v]){
  21.             dfsVisit(g, used, v, nodes_stack);
  22.         }
  23.     }
  24. }
  25.  
  26. void getTranspose(const vector<vector<int>> &g, vector<vector<int>> &gr){
  27.     for (int v = 0; v < g.size(); ++v){
  28.         for (const int &u : g[v]){
  29.             gr[u].push_back(v);
  30.         }
  31.     }
  32. }
  33.  
  34. void getComponent(const vector<vector<int>> &gr, vector<bool> &used, const int &v,
  35.         vector<int> &component, vector<int> &colors, const int &color){
  36.     used[v] = true;
  37.     colors[v] = color;
  38.     component.push_back(v);
  39.     for (const int &u : gr[v]){
  40.         if (!used[u]){
  41.             getComponent(gr, used, u, component, colors, color);
  42.         }
  43.     }
  44.  
  45. }
  46. void getSCC(const vector<vector<int>> &g, vector<vector<int>> &components, vector<int> &colors){
  47.         stack<int> nodes_stack;
  48.         getNodesStack(g, nodes_stack);
  49.         vector<vector<int>> gr(g.size());
  50.         getTranspose(g, gr);
  51.         vector<bool> used(gr.size(), false);
  52.         int color = 0;
  53.         while (!nodes_stack.empty()){
  54.             int v = nodes_stack.top();
  55.             nodes_stack.pop();
  56.             if (!used[v]){
  57.                 vector<int> newComponent;
  58.                 getComponent(gr, used, v, newComponent, colors, color++);
  59.                 components.push_back(newComponent);
  60.             }
  61.         }
  62. }
  63.  
  64. void printComponents(const vector<vector<int>> &components){
  65.     for (int i = 0; i < components.size(); ++i){
  66.         cout << "SCC " << i << ":   ";
  67.         for (auto v : components[i]){
  68.             cout << v << ' ';
  69.         }
  70.         cout << endl;
  71.     }
  72. }
  73.  
  74. void getMetaGraph(const vector<vector<int>> &g, const vector<int> &colors, vector<set<int>> &metaGraph){
  75.     for (int v = 0; v < g.size(); ++v){
  76.         for (auto u : g[v]){
  77.             if (colors[v] != colors[u]){
  78.                 metaGraph[colors[v]].insert(colors[u]);
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. void printMetaGraph(const vector<set<int>> &metaGraph){
  85.     cout << "Meta-graph:\n";
  86.     for (int v = 0; v < metaGraph.size(); ++v){
  87.         cout << v << "   ";
  88.         for (auto u : metaGraph[v]){
  89.             cout << u << ' ';
  90.         }
  91.         cout << endl;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement