Advertisement
Guest User

kusrach

a guest
Apr 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string>
  6. #include <cmath>
  7. #include <queue>
  8.  
  9. using namespace std;
  10.  
  11. #define forn(i, n) for(size_t i = 0; i < (n); i++)
  12. #define mp make_pair
  13. typedef pair<int, bool> pib;
  14. typedef pair<int, int> pii;
  15.  
  16. const int inf = 1000000000;
  17.  
  18. vector<vector<int> > g;
  19. vector<vector<int> > gtr;
  20. vector<vector<int> > paths;
  21. vector<int> dots;
  22. int n, m;
  23.  
  24. void front_find(int u, vector<int> &d, vector<bool> &used, queue<pib> &q, bool stop) {
  25.  
  26. if (stop) return;
  27.  
  28. for (size_t i = 0; i < g[u].size(); ++i) {
  29.  
  30. int to = g[u][i];
  31. if (d[to] == inf) {
  32. d[to] = d[u] + 1;
  33. paths[to].push_back(u);
  34. used[to] = true;
  35. q.push(mp(to, true));
  36. }
  37. else if (d[u] + 1 == d[to])
  38. paths[to].push_back(u);
  39. }
  40. }
  41.  
  42. void back_find(int u, vector<int> &d, vector<bool> &used, queue<pib> &q, bool &stop) {
  43.  
  44. for (size_t i = 0; i < gtr[u].size(); ++i) {
  45.  
  46. int to = gtr[u][i];
  47. if (used[to]) {
  48. paths[u].push_back(to); // Сомнительный момент, ответ из-за этого дублируется иногда
  49. dots.push_back(to + 1);
  50. stop = true;
  51. continue;
  52. }
  53. if (d[to] == inf && !stop) {
  54. d[to] = d[u] + 1;
  55. paths[u].push_back(to);
  56. q.push(mp(to, false));
  57. }
  58. }
  59. }
  60.  
  61. void find_path(int s, int f) {
  62.  
  63. queue<pib> q;
  64. vector<int> d(n, inf);
  65. vector<int> dtr(n, inf);
  66. paths.resize(n, vector<int>());
  67. bool stop = false;
  68.  
  69. vector<bool> used(n);
  70.  
  71. used[s] = true;
  72. used[f] = true;
  73.  
  74. d[s] = 0;
  75. dtr[f] = 0;
  76.  
  77. q.push(mp(s, true));
  78. q.push(mp(f, false));
  79. while (!q.empty()) {
  80. pib u = q.front();
  81. q.pop();
  82.  
  83. if (u.second)
  84. front_find(u.first, d, used, q, stop);
  85. else
  86. back_find(u.first, dtr, used, q, stop);
  87. }
  88. }
  89. int dp = 0;
  90. void create_paths(int u, vector<vector<int> > &result, vector<int> &arr, int deep) {
  91.  
  92. arr.push_back(u + 1);
  93. for (size_t i = 0; i < paths[u].size(); i++) {
  94. int to = paths[u][i];
  95. create_paths(to, result, arr, deep + 1);
  96. }
  97. dp = max(dp, deep);
  98. if (dp == deep)
  99. result.push_back(arr);
  100. arr.pop_back();
  101. }
  102.  
  103. void vec_print(vector<int>::iterator begin, vector<int>::iterator end) {
  104.  
  105. for (auto it = begin; it != end; it++)
  106. cout << *it << " ";
  107. cout << endl;
  108. }
  109.  
  110. int main() {
  111. //#ifdef _DEBUG
  112. freopen("input.txt", "r", stdin);
  113. freopen("output.txt", "w", stdout);
  114. //#endif
  115. ios_base::sync_with_stdio(false);
  116.  
  117. cin >> n >> m;
  118. int v1, v2;
  119. cin >> v1 >> v2;
  120. v1--, v2--;
  121.  
  122. g.resize(n, vector<int>());
  123. gtr.resize(n, vector<int>());
  124.  
  125. for (int i = 0; i < m; i++) {
  126. int x, y;
  127. cin >> x >> y;
  128. x--, y--;
  129. g[x].push_back(y);
  130. gtr[y].push_back(x);
  131. }
  132.  
  133. find_path(v1, v2);
  134.  
  135. vec_print(dots.begin(), dots.end());
  136.  
  137. vector<vector<int> > result;
  138. vector<int> temp;
  139. int deep = 0;
  140.  
  141. create_paths(v2, result, temp, deep);
  142.  
  143. for (size_t i = 0; i < result.size(); i++)
  144. vec_print(result[i].begin(), result[i].end());
  145.  
  146. return 0;
  147. }
  148.  
  149. /*
  150. input.txt
  151. 9 16
  152. 1 9
  153. 1 2
  154. 1 4
  155. 2 3
  156. 2 5
  157. 2 6
  158. 3 1
  159. 3 8
  160. 4 3
  161. 5 6
  162. 5 7
  163. 6 7
  164. 6 8
  165. 7 8
  166. 8 4
  167. 8 9
  168. 9 6
  169. */
  170.  
  171. /*
  172. output.txt
  173. 3 6
  174. 9 8 3 2 1
  175. 9 8 3 4 1
  176. 9 8 6 2 1
  177. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement