mr_dot_convict

259-Software-Allocation-UVa-mr.convict

May 19th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 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. const int N = 26 + 10 + 2;
  21. const int infi = (int)1e9;
  22.  
  23. int f[N][N];
  24. vector < vector <int> > Adj;
  25. int n, m;
  26. int Par[N];
  27.  
  28. int bfs (int s, int t) {
  29.    fill (Par, Par + 10 + 26 + 2, -1);
  30.    Par[s] = -2;
  31.  
  32.    queue <pii> Q;
  33.    Q.push(pii(s, infi));
  34.  
  35.    while (!Q.empty()) {
  36.       pii tmp = Q.front();
  37.       Q.pop();
  38.       int u = tmp.x, flow = tmp.y;
  39.  
  40.       for (int v : Adj[u]) {
  41.          if (Par[v] == -1 && f[u][v]) {
  42.             Par[v] = u;
  43.             int newFlow = min(flow, f[u][v]);
  44.             if (v == t) {
  45.                return newFlow;
  46.             }
  47.             Q.push(pii(v, newFlow));
  48.          }
  49.       }
  50.    }
  51.    return 0;
  52. }
  53.  
  54. bool edmondsKarp(int s, int t, int K) {
  55.    int flow = 0;
  56.    int newFlow;
  57.  
  58.    while ((newFlow = bfs (s, t))) {
  59.       flow += newFlow;
  60.       int cur = t;
  61.       while (cur != s) {
  62.          int prv = Par[cur];
  63.          f[prv][cur] -= newFlow;
  64.          f[cur][prv] += newFlow;
  65.          cur = prv;
  66.       }
  67.    }
  68.    if (flow != K) return false;
  69.  
  70.    for (int v = 0; v < 10; ++v) {
  71.       bool ok = false;
  72.       for (int u = 10; u < 10 + 26; ++u) {
  73.          if (f[v][u] == 1) {
  74.             cout << char(u - 10 + 'A');
  75.             ok = true;
  76.          }
  77.       }
  78.       if (!ok) cout << "_";
  79.    }
  80.    cout << '\n';
  81.    return true;
  82. }
  83.  
  84. void read() {
  85.    string st;
  86.    int s = 10 + 26, t = 10 + 26 + 1, K = 0;
  87.    Adj.assign(26 + 10 + 2, vector <int> ());
  88.    bool new_test = true;
  89.  
  90.    while (true) {
  91.       getline(cin, st);
  92.       bool eoff = !cin;
  93.       if (eoff || st[0] - 'A' == -65) {
  94.          if (!edmondsKarp(s, t, K)) cout << "!\n";
  95.          new_test = true;
  96.          if (eoff) break;
  97.          continue;
  98.       }
  99.       if (new_test) {
  100.          new_test = false;
  101.          Adj.assign(10 + 26 + 2, vector <int> ());
  102.  
  103.          for (int u = 0; u < 10; ++u) {
  104.             Adj[t].push_back(u);
  105.             Adj[u].push_back(t);
  106.          }
  107.  
  108.          for (int v = 10; v < 10 + 26; ++v) {
  109.             Adj[v].push_back(s);
  110.             Adj[s].push_back(v);
  111.          }
  112.          for (int u = 0; u < (10 + 26 + 2); ++u) {
  113.             for (int v = 0; v < (10 + 26 + 2); ++v) {
  114.                f[u][v] = 0, f[v][u] = 0;
  115.             }
  116.          }
  117.          K = 0;
  118.       }
  119.  
  120.       int u = st[0] - 'A' + 10, k = st[1] - '0';
  121.       f[s][u] = k;
  122.       K += k;
  123.  
  124.       assert(st[2] == ' ');
  125.  
  126.       int v;
  127.       for (int i = 3; st[i] != ';'; ++i) {
  128.          v = st[i] - '0';
  129.          f[u][v] = infi;
  130.          f[v][t] = 1;
  131.          Adj[u].push_back(v);
  132.          Adj[v].push_back(u);
  133.       }
  134.    }
  135. }
  136.  
  137. signed main() {
  138.    read();
  139.    return EXIT_SUCCESS;
  140. }
Add Comment
Please, Sign In to add comment