Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <set>
- #include <vector>
- using namespace std;
- class Graph
- {
- private:
- int V;
- vector< multiset<int> > adjustence;
- public:
- Graph(int v) : adjustence(v)
- {
- V = v;
- }
- Graph(string path)
- {
- int v, w;
- ifstream input(path);
- input >> V;
- adjustence.resize(V);
- while(input >> v >> w)
- {
- AddEdge(v, w);
- }
- }
- void AddEdge(int v, int w)
- {
- adjustence[v].insert(w);
- }
- int GetSize()
- {
- return V;
- }
- multiset<int> GetAdjustence(int v) {
- return adjustence[v];
- }
- };
- class Pathes
- {
- private:
- vector<bool> marked;
- vector<int> edgeTo;
- int source;
- Graph graph;
- void innerDfs(int currentVertex, int previousVertex)
- {
- marked[currentVertex] = true;
- edgeTo[currentVertex] = previousVertex;
- for(auto i : graph.GetAdjustence(currentVertex))
- {
- innerDfs(*i, currentVertext);
- }
- }
- void dfs()
- {
- innerDfs(source, source);
- }
- public:
- Pathes(Graph g, int source) : marked(g.GetSize()), edgeTo(g.GetSize())
- {
- graph = g;
- this.source = source;
- dfs();
- }
- vector<int> getPathTo(int w)
- {
- vector<int> path;
- int currentVertex = w;
- while(edgeTo[currentVertex] != w)
- {
- }
- }
- bool hasPathTo(int w)
- {
- return marked[w];
- }
- }
- int main() {
- Graph g("input.txt");
- for(int v = 0; v < g.GetSize(); v++)
- {
- for(auto i : g.GetAdjustence(v))
- cout << i << " ";
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement