mr_dot_convict

663-Sorting-Slides-UVa-mr.convict

May 27th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 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. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19.  
  20. #define debug(args...) { \
  21.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  22.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  23. }
  24. void err(istream_iterator<string> it) { it->empty();
  25.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  26. }
  27. template<typename T, typename... Args>
  28. void err(istream_iterator<string> it, T a, Args... args) {
  29.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  30.     err(++it, args...);
  31. }
  32. const int N = 2 * 26 + 10;
  33.  
  34. struct Rect {
  35.    int x_min, x_max, y_min, y_max;
  36.    inline bool inside (int xx, int yy) {
  37.       return xx > x_min && xx < x_max && yy > y_min && yy < y_max;
  38.    }
  39. };
  40.  
  41. struct Edge {
  42.    int u, v;
  43.    Edge(int _u = -1, int _v = -1) : u(_u), v(_v) {};
  44.    inline bool operator == (const Edge &o) const {
  45.       return u == o.u && v == o.v;
  46.    }
  47. };
  48.  
  49. vector <Rect> rect;
  50. bool vis[N];
  51. int match[N];
  52. vector < vector <int> > Adj;
  53. int n, nl, nr; // first 0, ..., nl - 1 vertices in left nl, ..., nr - 1 in right
  54.  
  55. inline void re_init() {
  56.    for (int i = 0; i < n; ++i) {
  57.       vis[i] = false, match[i] = -1;
  58.    }
  59. }
  60.  
  61. void init() {
  62.    n = nl + nr;
  63.    assert(n);
  64.    rect.clear();
  65.    Adj.assign(n, vector <int> ());
  66.    re_init();
  67. }
  68.  
  69. inline void add_edge(int u, int v) {
  70.    Adj[u].push_back(v);
  71. }
  72.  
  73. bool augment(int u, const Edge &ban) {
  74.    if (vis[u]) return false;
  75.    vis[u] = true;
  76.  
  77.    for (int v : Adj[u]) {
  78.       if (Edge(u, v) == ban) continue;
  79.       if (match[v] == -1 || augment(match[v], ban)) {
  80.          match[v] = u;
  81.          return true;
  82.       }
  83.    }
  84.    return false;
  85. }
  86.  
  87. int MCBM(const Edge &ban = Edge()) {
  88.    assert (nl + nr == n);
  89.    int mcbm = 0;
  90.  
  91.    for (int u = 0; u < nl; ++u) { // in left set
  92.       fill(vis, vis + n, false);
  93.       mcbm += augment(u, ban);
  94.    }
  95.    return mcbm;
  96. }
  97.  
  98. void solve(int tc) {
  99.    cout << "Heap " << tc << '\n';
  100.    using pci = pair <char, int>;
  101.    vector <pci> ans;
  102.  
  103.    for (int u = 0; u < nl; ++u) {
  104.       for (int v : Adj[u]) {
  105.          re_init();
  106.          int flow_ban_uv = MCBM(Edge(u, v));
  107.          //debug(u, v, flow_ban_uv);
  108.          if (flow_ban_uv != nl) {
  109.             ans.push_back(pci(char('A' + u), v + 1));
  110.          }
  111.       }
  112.    }
  113.  
  114.    if (ans.size())
  115.       for (int i = 0; i < (int)ans.size(); ++i)
  116.          cout << "(" << ans[i].x << ',' << ans[i].y << ")" <<
  117.             " \n"[ i == (int)ans.size() - 1];
  118.    else cout << "none\n";
  119.    cout << '\n';
  120. }
  121.  
  122. void read() {
  123.    nr = nl;
  124.    init();
  125.  
  126.    int x_min, x_max, y_min, y_max;
  127.    for (int i = 0; i < nl; ++i) {
  128.       cin >> x_min >> x_max >> y_min >> y_max;
  129.       rect.push_back({x_min, x_max, y_min, y_max});
  130.    }
  131.  
  132.    int xx, yy;
  133.    for (int i = 0; i < nr; ++i) {
  134.       cin >> xx >> yy;
  135.       for (int j = 0; j < nl; ++j) {
  136.          if (rect[j].inside(xx, yy))
  137.             add_edge(j, i);
  138.       }
  139.    }
  140. }
  141.  
  142. signed main() {
  143.    IOS; PREC;
  144.    int tc = 1;
  145.    while (cin >> nl, nl) {
  146.       read();
  147.       solve(tc++);
  148.    }
  149.  
  150.    return EXIT_SUCCESS;
  151. }
Add Comment
Please, Sign In to add comment