Advertisement
pasholnahuy

Untitled

Oct 15th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <map>
  5. #include <set>
  6. #include <bit>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <exception>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. struct Edge {
  15. int a;
  16. int b;
  17. int capacity;
  18. int flow = 0;
  19.  
  20. int other(int v) const {
  21. if (v == a) {
  22. return b;
  23. }
  24. return a;
  25. }
  26.  
  27. int capacityTo(int v) const {
  28. if (v == a) {
  29. return flow;
  30. }
  31. return capacity - flow;
  32. }
  33.  
  34. void addFlowTo(int v, int delta) {
  35. if (v == a) {
  36. flow -= delta;
  37. } else {
  38. flow += delta;
  39. }
  40. }
  41. };
  42.  
  43. class Graph {
  44. vector<Edge> edges;
  45. vector<vector<int>> graph;
  46. vector<int> parents;
  47.  
  48. void bfs(int start, int finish) {
  49. queue<int> q;
  50. q.push(start);
  51. while (!q.empty() && parents[finish] == -1) {
  52. const auto vertex = q.front();
  53. q.pop();
  54. for (const auto e: graph[vertex]) {
  55. // 1e9 means unreached
  56. int to = edges[e].other(vertex);
  57. if (parents[to] == -1 && edges[e].capacityTo(to)) {
  58. parents[to] = e;
  59. q.push(to);
  60. }
  61. }
  62. }
  63. }
  64.  
  65.  
  66. int hasPath(int start, int finish) {
  67. parents.assign(graph.size(), -1);
  68. bfs(start, finish);
  69. if (parents[finish] != -1) {
  70. int min_capacity = edges[parents[finish]].capacityTo(finish);
  71. for (int v = finish; v != start; v = edges[parents[v]].other(v)) {
  72. auto cur_capacity = edges[parents[v]].capacityTo(v);
  73. if (min_capacity > cur_capacity) {
  74. min_capacity = cur_capacity;
  75. }
  76. }
  77. return min_capacity;
  78. } else {
  79. return -1;
  80. }
  81. }
  82.  
  83.  
  84. void addFlow(int start, int finish, int deltaFlow) {
  85. for (int v = finish; v != start; v = edges[parents[v]].other(v))
  86. edges[parents[v]].addFlowTo(v, deltaFlow);
  87. }
  88.  
  89. public:
  90.  
  91. Graph( vector<Edge> edges, vector<vector<int>> graph) :
  92. edges(edges), graph(graph) {}
  93.  
  94.  
  95. long long maxFlow(int start, int finish) {
  96. long long flow = 0;
  97. int delta = hasPath(start, finish);
  98. while (delta != -1) {
  99. addFlow(start, finish, delta);
  100. flow += delta;
  101. delta = hasPath(start, finish);
  102. }
  103. return flow;
  104. }
  105.  
  106. vector<int> bfs_ans(int start, int finish) {
  107. parents.assign(graph.size(), -1);
  108. queue<int> q;
  109. q.push(start);
  110. while (!q.empty() && parents[finish] == -1) {
  111. const auto vertex = q.front();
  112. q.pop();
  113. for (const auto e: graph[vertex]) {
  114. // 1e9 means unreached
  115. int to = edges[e].other(vertex);
  116. if (parents[to] == -1 && edges[e].flow > 0) {
  117. parents[to] = e;
  118. q.push(to);
  119. }
  120. }
  121. }
  122. vector<int> way;
  123. int cur = finish;
  124. way.push_back(finish);
  125. while (cur != start) {
  126. edges[parents[cur]].flow = 0;
  127. cur = edges[parents[cur]].other(cur);
  128. way.push_back(cur);
  129. }
  130. reverse(way.begin(), way.end());
  131. return way;
  132. }
  133.  
  134. };
  135.  
  136. int main() {
  137. int n_vertexes, n_edges;
  138. cin >> n_vertexes >> n_edges;
  139. int start, finish;
  140. cin >> start >> finish;
  141. --start;
  142. --finish;
  143. vector<Edge> edges;
  144. vector<vector<int>> graph(n_vertexes);
  145. for (size_t i = 0; i < n_edges; ++i) {
  146. int from, to;
  147. cin >> from >> to;
  148. --from;
  149. --to;
  150. edges.push_back({from, to, 1});
  151. graph[from].push_back(edges.size() - 1);
  152. graph[to].push_back(edges.size() - 1);
  153. }
  154. Graph g( edges, graph);
  155. auto f = g.maxFlow(start, finish);
  156. if (f < 2) {
  157. cout << "NO";
  158. } else {
  159. cout << "YES" << "\n";
  160. for (auto el: g.bfs_ans(start, finish)) {
  161. cout << el+1 << " ";
  162. }
  163. cout << "\n";
  164. for (auto el: g.bfs_ans(start, finish)) {
  165. cout << el+1 << " ";
  166. }
  167. cout << "\n";
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement