Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<vector>
- #include<iomanip>
- using namespace std;
- vector<int> graph[100];
- int size=0;
- void loadGraph(char* name)
- {
- fstream stream(name);
- int startTop, endTop;
- while (stream >> startTop >> endTop)
- {
- graph[startTop].push_back(endTop);
- if (size < startTop)
- {
- size = startTop+1;
- }
- }
- }
- bool hasMirorEdge(int n,int i)
- {
- for (int j = 0; j < graph[i].size(); j++)
- {
- if (graph[i][j] == n)
- {
- return true;
- }
- }
- return false;
- }
- void hasTheMirorPath(int n,int m, int length,bool& checker)
- {
- if (checker)
- {
- return;
- }
- if (length<=0)
- {
- checker = true;
- return;
- }
- for (int i = 0; i < graph[n].size(); i++)
- {
- if (hasMirorEdge(n, graph[n][i]))
- {
- hasTheMirorPath(graph[n][i], m, length - 1, checker);
- }
- }
- }
- bool maxTwoWayFrom(int n,int length)
- {
- bool checker = false;
- hasTheMirorPath(n, n, length, checker);
- return checker;
- }
- void printGraph()
- {
- for (int i = 0; i < size+1; i++)
- {
- if (graph[i].size() == 0)
- {
- continue;
- }
- else
- {
- cout << i << ": ";
- for (int j = 0; j < graph[i].size(); j++)
- {
- cout << graph[i][j] << " ";
- }
- cout << endl;
- }
- }
- }
- int main()
- {
- int counter = 0;
- loadGraph("text.txt");
- printGraph();
- cout << boolalpha << maxTwoWayFrom(1, 3) << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement