Advertisement
pasholnahuy

Untitled

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