Advertisement
Guest User

MirrorPath

a guest
Feb 11th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<iomanip>
  5. using namespace std;
  6. vector<int> graph[100];
  7. int size=0;
  8.  
  9. void loadGraph(char* name)
  10. {
  11. fstream stream(name);
  12. int startTop, endTop;
  13. while (stream >> startTop >> endTop)
  14. {
  15. graph[startTop].push_back(endTop);
  16. if (size < startTop)
  17. {
  18. size = startTop+1;
  19. }
  20. }
  21.  
  22. }
  23.  
  24. bool hasMirorEdge(int n,int i)
  25. {
  26. for (int j = 0; j < graph[i].size(); j++)
  27. {
  28. if (graph[i][j] == n)
  29. {
  30. return true;
  31. }
  32. }
  33.  
  34. return false;
  35. }
  36.  
  37. void hasTheMirorPath(int n,int m, int length,bool& checker)
  38. {
  39. if (checker)
  40. {
  41. return;
  42. }
  43.  
  44. if (length<=0)
  45. {
  46. checker = true;
  47. return;
  48. }
  49.  
  50. for (int i = 0; i < graph[n].size(); i++)
  51. {
  52. if (hasMirorEdge(n, graph[n][i]))
  53. {
  54. hasTheMirorPath(graph[n][i], m, length - 1, checker);
  55. }
  56. }
  57. }
  58.  
  59. bool maxTwoWayFrom(int n,int length)
  60. {
  61. bool checker = false;
  62. hasTheMirorPath(n, n, length, checker);
  63. return checker;
  64.  
  65. }
  66.  
  67. void printGraph()
  68. {
  69. for (int i = 0; i < size+1; i++)
  70. {
  71. if (graph[i].size() == 0)
  72. {
  73. continue;
  74. }
  75. else
  76. {
  77. cout << i << ": ";
  78. for (int j = 0; j < graph[i].size(); j++)
  79. {
  80. cout << graph[i][j] << " ";
  81. }
  82.  
  83. cout << endl;
  84. }
  85. }
  86. }
  87.  
  88. int main()
  89. {
  90. int counter = 0;
  91. loadGraph("text.txt");
  92. printGraph();
  93. cout << boolalpha << maxTwoWayFrom(1, 3) << endl;
  94.  
  95. system("pause");
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement