playerr17

Untitled

May 6th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <vector>
  5. #include <numeric>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <cmath>
  10. #include <stack>
  11. #include <deque>
  12. #include <string>
  13. #include <ctime>
  14. #include <bitset>
  15. #include <queue>
  16. #include <cassert>
  17. #include<unordered_set>
  18. #include<unordered_map>
  19. #include<string.h>
  20. #include <random>
  21. #include <chrono>
  22. #include <math.h>
  23. using namespace std;
  24. #define ll long long
  25. #define ld long double
  26. #define pi pair<int, int>
  27. #define pll pair<ll, ll>
  28. #define mint map<int, int>*;
  29. #define vi vector<int>
  30. #define int long long
  31. #pragma GCC optimize("O3,unroll-loops")
  32. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  33. template<typename T>
  34. istream& operator>>(istream& is, vector<T>& v) {
  35.     for (T& o : v) {
  36.         is >> o;
  37.     }
  38.     return is;
  39. }
  40. class Query {
  41. public:
  42.     int l = 0;
  43.     int r = 0;
  44.     int idx = 0;
  45.     Query(int L, int R, int Num) {
  46.         l = L;
  47.         r = R;
  48.         idx = Num;
  49.     }
  50. };
  51. int NOD(int a, int b) {
  52.     if (a < 0 || b < 0) return NOD(abs(a), abs(b));
  53.     return b ? NOD(b, a % b) : a;
  54. }
  55. const ld EPS = 1e-6;
  56. const ll INF = 1e16 + 3;
  57. const ll MOD1 = 1e9 + 7;
  58. const ll POWER = 5;
  59. const ll HASHMOD1 = 939999971;
  60. const ll HASHMOD2 = 23;
  61. const ll MOD2 = 1e9 + 7;
  62. const ld PHI = atan(1) * 4;
  63. const ll MOD3 = 998244353;
  64. const int root = 31;
  65. const int root_1 = 128805723;
  66. const int root_pw = 1 << 23;
  67. vector<int> dX = { 1, 0, -1, 0, };
  68. vector<int> dY = { 0, 1, 0, -1 };
  69. mt19937 MT(chrono::steady_clock::now().time_since_epoch().count());
  70. const int block_size = 500;
  71. const int maxN = 1e6 + 5;
  72.  
  73. vector<vector<int>> g;
  74. vector<int> d;
  75. vector<int> ptr;
  76.  
  77. struct Edge {
  78. public:
  79.     int u = 0; int v = 0;
  80.     int cap = 0; int flow = 0;
  81.     Edge(int v1, int v2, int capacity, int Flow) {
  82.         flow = Flow; u = v1; v = v2; cap = capacity;
  83.     }
  84. };
  85. vector<Edge> allEdges;
  86. void addNewEdge(int v1, int v2, int capacity) {
  87.     Edge eTo = Edge(v1, v2, capacity, 0);
  88.     Edge eBack = Edge(v2, v1, 0, 0);
  89.     g[v1].push_back(allEdges.size());
  90.     allEdges.push_back(eTo);
  91.     g[v2].push_back(allEdges.size());
  92.     allEdges.push_back(eBack);
  93. }
  94.  
  95. bool BFSFLOW(int s, int t, int limit, int N) {
  96.     d.assign(N + 1, INF);
  97.     queue<int> q;
  98.     q.push(s); d[s] = 0;
  99.     while (!q.empty() && d[t] == INF) {
  100.         int v = q.front(); q.pop();
  101.         for (int j = 0; j < g[v].size(); ++j) {
  102.             int id = g[v][j];
  103.             int goingTo = allEdges[id].v;
  104.             if (d[goingTo] == INF && allEdges[id].flow < allEdges[id].cap && allEdges[id].cap - allEdges[id].flow >= limit) {
  105.                 q.push(goingTo);
  106.                 d[goingTo] = d[v] + 1;
  107.             }
  108.         }
  109.     }
  110.     return d[t] != INF;
  111. }
  112.  
  113. vector<int> ans;
  114. vector<vector<int>> dek;
  115.  
  116. int DFSFLOW(int v, int flow, int t) {
  117.     if (!flow) return 0;
  118.     if (v == t) return flow;
  119.     for (; ptr[v] < g[v].size(); ptr[v]++) {
  120.         int newId = g[v][ptr[v]]; int TO = allEdges[newId].v;
  121.         if (d[TO] != d[v] + 1) continue;
  122.         int tmp = allEdges[newId].cap - allEdges[newId].flow;
  123.         int pushed = DFSFLOW(TO, min(flow, tmp), t);
  124.         if (pushed) {
  125.             allEdges[newId].flow += pushed;
  126.             allEdges[newId ^ 1].flow -= pushed;
  127.             return pushed;
  128.         }
  129.     }
  130.     return 0;
  131. }
  132.  
  133. int dinic(int s, int t, int N) {
  134.     int flow = 0;
  135.     int k = (1 << 30);
  136.     while (k > 0) {
  137.         while (BFSFLOW(s, t, k, N)) {
  138.             ptr.assign(N + 1, 0);
  139.             while (int pushed = DFSFLOW(s, INF, t)) {
  140.                 flow += pushed;
  141.             }
  142.         }
  143.         k /= 2;
  144.     }
  145.     return flow;
  146. }
  147.  
  148. int dfs(int v, int flow, int t) {
  149.     if (!flow) return 0;
  150.     if (v == t) return flow;
  151.     for (int u = 0; u < g[v].size(); u++) {
  152.         int newId = g[v][u]; int TO = allEdges[newId].v;
  153.         int tmp = allEdges[newId].flow;
  154.         if (tmp <= 0) {
  155.             continue;
  156.         }
  157.         int pushed = dfs(TO, min(flow, tmp), t);
  158.         if (pushed) {
  159.             dek[ans.size()].push_back((newId / 2) + 1);
  160.             allEdges[newId].flow -= pushed;
  161.             allEdges[newId ^ 1].flow += pushed;
  162.             return pushed;
  163.         }
  164.     }
  165.     return 0;
  166. }
  167.  
  168. void dekomposition(int n) {
  169.     int fl = dfs(1, INF, n);
  170.     while (fl != 0) {
  171.         ans.push_back(fl);
  172.         dek.emplace_back();
  173.         fl = dfs(1, INF, n);
  174.     }
  175. }
  176.  
  177.  
  178. void solve() {
  179.     int N = 0, M = 0; cin >> N >> M;
  180.     dek.emplace_back();
  181.     g.assign(N + 1, vector<int>());
  182.     for (int j = 0; j < M; ++j) {
  183.         int a = 0, b = 0, cap = 0; cin >> a >> b >> cap;
  184.         addNewEdge(a, b, cap);
  185.     }
  186.     dinic(1, N, N);
  187.  
  188.     dekomposition(N);
  189.  
  190.     cout << ans.size() << endl;
  191.     for (int i = 0; i < ans.size(); ++i) {
  192.         cout << ans[i] << " " << dek[i].size() << " ";
  193.         reverse(dek[i].begin(), dek[i].end());
  194.         for (auto j : dek[i]) {
  195.             cout << j << " ";
  196.         }
  197.         cout << endl;
  198.     }
  199. }
  200.  
  201. signed main() {
  202.     ios_base::sync_with_stdio(false);
  203.     cin.tie(0);
  204.     cout.tie(0);
  205.     cout << setprecision(12);
  206.     int T = 1;
  207.     while (T--) {
  208.         solve();
  209.     }
  210.     return 0;
  211. }
  212.  
Add Comment
Please, Sign In to add comment