Advertisement
Guest User

123

a guest
Jan 18th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <set>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. class Graph
  8. {
  9.     private:
  10.         int V;
  11.         vector< multiset<int> > adjustence;
  12.     public:
  13.         Graph(int v) : adjustence(v)
  14.         {
  15.             V = v;
  16.         }
  17.        
  18.         Graph(string path)
  19.         {
  20.             int v, w;
  21.             ifstream input(path);
  22.             input >> V;
  23.             adjustence.resize(V);
  24.             while(input >> v >> w)
  25.             {
  26.                 AddEdge(v, w);
  27.             }
  28.         }
  29.  
  30.         void AddEdge(int v, int w)
  31.         {
  32.             adjustence[v].insert(w);
  33.         }
  34.  
  35.         int GetSize()
  36.         {
  37.             return V;
  38.         }
  39.  
  40.         multiset<int> GetAdjustence(int v) {
  41.             return adjustence[v];
  42.         }
  43. };
  44.  
  45. class Pathes
  46. {
  47.     private:
  48.         vector<bool> marked;
  49.         vector<int> edgeTo;
  50.         int source;
  51.         Graph graph;
  52.  
  53.         void innerDfs(int currentVertex, int previousVertex)
  54.         {
  55.              marked[currentVertex] = true;
  56.              edgeTo[currentVertex] = previousVertex;
  57.              for(auto i : graph.GetAdjustence(currentVertex))
  58.              {
  59.                 innerDfs(*i, currentVertext);
  60.              }
  61.  
  62.         }
  63.  
  64.         void dfs()
  65.         {
  66.             innerDfs(source, source);
  67.         }
  68.     public:
  69.         Pathes(Graph g, int source) : marked(g.GetSize()), edgeTo(g.GetSize())
  70.         {
  71.             graph = g;
  72.             this.source = source;
  73.             dfs();
  74.         }
  75.  
  76.         vector<int> getPathTo(int w)
  77.         {
  78.             vector<int> path;
  79.             int currentVertex = w;
  80.             while(edgeTo[currentVertex] != w)
  81.             {
  82.                
  83.             }
  84.         }
  85.  
  86.  
  87.         bool hasPathTo(int w)
  88.         {
  89.             return marked[w];
  90.         }
  91. }
  92.  
  93. int main() {
  94.     Graph g("input.txt");
  95.     for(int v = 0; v < g.GetSize(); v++)
  96.     {
  97.         for(auto i : g.GetAdjustence(v))
  98.             cout << i << " ";
  99.          cout << endl;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement