mr_dot_convict

10480-Sabotage-UVa-mr.convict

May 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. #pragma GCC      optimize ("Ofast")
  10. #pragma GCC      optimize ("unroll-loops")
  11. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  12.  
  13. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC     cout.precision (10); cout << fixed
  15. #define bg(x)    "[ " << #x << " : " << x << " ]"
  16. #define x        first
  17. #define y        second
  18. using namespace std;
  19. using pii = pair <int,int>;
  20.  
  21. const int N = 55;
  22. const int infi = (int)1e9;
  23.  
  24. int f[N][N];
  25. vector < vector <int> > Adj;
  26. int n, m;
  27. int Par[N];
  28.  
  29. int bfs (int s, int t) {
  30.    fill (Par, Par + n, -1);
  31.    Par[s] = -2;
  32.  
  33.    queue <pii> Q;
  34.    Q.push(pii(s, infi));
  35.  
  36.    while (!Q.empty()) {
  37.       pii tmp = Q.front();
  38.       Q.pop();
  39.       int u = tmp.x, flow = tmp.y;
  40.  
  41.       for (int v : Adj[u]) {
  42.          if (Par[v] == -1 && f[u][v]) {
  43.             Par[v] = u;
  44.             int newFlow = min(flow, f[u][v]);
  45.             if (v == t) {
  46.                return newFlow;
  47.             }
  48.             Q.push(pii(v, newFlow));
  49.          }
  50.       }
  51.    }
  52.    return 0;
  53. }
  54.  
  55. int edmondsKarp(int s, int t) {
  56.    int flow = 0;
  57.    int newFlow;
  58.  
  59.    while ((newFlow = bfs (s, t))) {
  60.       flow += newFlow;
  61.       int cur = t;
  62.       while (cur != s) {
  63.          int prv = Par[cur];
  64.          f[prv][cur] -= newFlow;
  65.          f[cur][prv] += newFlow;
  66.          cur = prv;
  67.       }
  68.    }
  69.    return flow;
  70. }
  71.  
  72. void dfs (int u, bool inS[]) {
  73.    inS[u] = true;
  74.    for (int v : Adj[u]) {
  75.       if (!inS[v] && f[u][v]) {
  76.          dfs (v, inS);
  77.       }
  78.    }
  79. }
  80.  
  81. inline void addEdge(int u, int v, int flow) {
  82.    Adj[u].push_back(v);
  83.    Adj[v].push_back(u);
  84.    f[u][v] = flow;
  85.    f[v][u] = flow;
  86. }
  87.  
  88. void solve() {
  89.    Adj.assign(N, vector <int> ());
  90.  
  91.    int s = 0, t = 1;
  92.    for (int e = 0; e < m; ++e) {
  93.       int u, v, c;
  94.       cin >> u >> v >> c;
  95.       --u, --v;
  96.       addEdge(u, v, c);
  97.    }
  98.  
  99.    int flow = edmondsKarp(s, t);
  100.    //cerr << flow << '\n';
  101.    bool inS[N] = {0};
  102.    dfs (s, inS);
  103.  
  104.    vector <pii> cutEdges;
  105.    for (int u = 0; u < n; ++u) {
  106.       if (!inS[u]) continue;
  107.       for (int v : Adj[u]) {
  108.          if (inS[v]) continue;
  109.          assert(f[u][v] == 0);
  110.          cutEdges.push_back(pii(u + 1, v + 1));
  111.       }
  112.    }
  113.  
  114.    for (pii cut : cutEdges) {
  115.       cout << cut.x << ' ' << cut.y << '\n';
  116.    }
  117.    cout << '\n';
  118. }
  119.  
  120. signed main() {
  121.    while (cin >> n >> m, n || m) {
  122.       solve();
  123.    }
  124.  
  125.    return EXIT_SUCCESS;
  126. }
Add Comment
Please, Sign In to add comment