Advertisement
pasholnahuy

Untitled

Oct 15th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 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. int checkFLow(int v){
  42. return v == a ? 0 : flow;
  43. }
  44. };
  45.  
  46. class Graph {
  47. vector<Edge> edges;
  48. vector<vector<int>> graph;
  49. vector<int> parents;
  50.  
  51. void bfs(int start, int finish) {
  52. queue<int> q;
  53. q.push(start);
  54. while (!q.empty() && parents[finish] == -1) {
  55. const auto vertex = q.front();
  56. q.pop();
  57. for (const auto e: graph[vertex]) {
  58. // 1e9 means unreached
  59. int to = edges[e].other(vertex);
  60. if (parents[to] == -1 && edges[e].capacityTo(to)) {
  61. parents[to] = e;
  62. q.push(to);
  63. }
  64. }
  65. }
  66. }
  67.  
  68.  
  69. int hasPath(int start, int finish) {
  70. parents.assign(graph.size(), -1);
  71. bfs(start, finish);
  72. if (parents[finish] != -1) {
  73. int min_capacity = edges[parents[finish]].capacityTo(finish);
  74. for (int v = finish; v != start; v = edges[parents[v]].other(v)) {
  75. auto cur_capacity = edges[parents[v]].capacityTo(v);
  76. if (min_capacity > cur_capacity) {
  77. min_capacity = cur_capacity;
  78. }
  79. }
  80. return min_capacity;
  81. } else {
  82. return -1;
  83. }
  84. }
  85.  
  86.  
  87. void addFlow(int start, int finish, int deltaFlow) {
  88. for (int v = finish; v != start; v = edges[parents[v]].other(v))
  89. edges[parents[v]].addFlowTo(v, deltaFlow);
  90. }
  91.  
  92. public:
  93.  
  94. Graph( vector<Edge> edges, vector<vector<int>> graph) :
  95. edges(edges), graph(graph) {}
  96.  
  97.  
  98. long long maxFlow(int start, int finish) {
  99. long long flow = 0;
  100. int delta = hasPath(start, finish);
  101. while (delta != -1) {
  102. addFlow(start, finish, delta);
  103. flow += delta;
  104. delta = hasPath(start, finish);
  105. }
  106. return flow;
  107. }
  108.  
  109. vector<int> bfs_ans(int start, int finish) {
  110. parents.assign(graph.size(), -1);
  111. queue<int> q;
  112. q.push(start);
  113. while (!q.empty() && parents[finish] == -1) {
  114. const auto vertex = q.front();
  115. q.pop();
  116. for (const auto e: graph[vertex]) {
  117. // 1e9 means unreached
  118. int to = edges[e].other(vertex);
  119. if (parents[to] == -1 && edges[e].checkFLow(to) > 0) {
  120. parents[to] = e;
  121. q.push(to);
  122. }
  123. }
  124. }
  125. vector<int> way;
  126. int cur = finish;
  127. way.push_back(finish);
  128. while (cur != start) {
  129. edges[parents[cur]].flow = 0;
  130. cur = edges[parents[cur]].other(cur);
  131. way.push_back(cur);
  132. }
  133. reverse(way.begin(), way.end());
  134. return way;
  135. }
  136.  
  137. };
  138.  
  139. int main() {
  140. int n_vertexes, n_edges;
  141. cin >> n_vertexes >> n_edges;
  142. int start, finish;
  143. cin >> start >> finish;
  144. --start;
  145. --finish;
  146. vector<Edge> edges;
  147. vector<vector<int>> graph(n_vertexes);
  148. for (size_t i = 0; i < n_edges; ++i) {
  149. int from, to;
  150. cin >> from >> to;
  151. --from;
  152. --to;
  153. edges.push_back({from, to, 1});
  154. graph[from].push_back(edges.size() - 1);
  155. graph[to].push_back(edges.size() - 1);
  156. }
  157. Graph g( edges, graph);
  158. auto f = g.maxFlow(start, finish);
  159. if (f < 2) {
  160. cout << f << " NO";
  161. } else {
  162. cout << "YES" << "\n";
  163. for (auto el: g.bfs_ans(start, finish)) {
  164. cout << el+1 << " ";
  165. }
  166. cout << "\n";
  167. for (auto el: g.bfs_ans(start, finish)) {
  168. cout << el+1 << " ";
  169. }
  170. cout << "\n";
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement