mr_dot_convict

10349-Antenna-Placement-UVa-mr.convict

May 27th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 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 = 40 * 10;
  33.  
  34. vector <string> input;
  35. bool vis[N];
  36. int match[N];
  37. vector < vector <int> > Adj;
  38. int n, nl, nr; // first 0, ..., nl - 1 vertices in left nl, ..., nr - 1 in right
  39. int mp[N];
  40.  
  41.  
  42. void re_init() {
  43.    for (int i = 0; i < n; ++i)
  44.       vis[i] = false, match[i] = -1;
  45. }
  46.  
  47. void init() {
  48.    Adj.assign(N, vector <int> ());
  49.    input.clear();
  50.    for (int i = 0; i < N; ++i) mp[i] = -1;
  51. }
  52.  
  53. inline void add_edge(int u, int v) {
  54.    Adj[u].push_back(v);
  55. }
  56.  
  57. bool augment(int u) {
  58.    if (vis[u]) return false;
  59.    vis[u] = true;
  60.  
  61.    for (int v : Adj[u]) {
  62.       if (match[v] == -1 || augment(match[v])) {
  63.          match[v] = u;
  64.          return true;
  65.       }
  66.    }
  67.    return false;
  68. }
  69.  
  70. int MCBM() {
  71.    assert (nl + nr == n);
  72.    int mcbm = 0;
  73.  
  74.    for (int u = 0; u < nl; ++u) { // in left set
  75.       fill(vis, vis + n, false);
  76.       mcbm += augment(u);
  77.    }
  78.    return mcbm;
  79. }
  80.  
  81. int h, w;
  82. void read() {
  83.    init();
  84.    cin >> h >> w;
  85.    string st;
  86.    for (int i = 0; i < h; ++i) {
  87.       cin >> st;
  88.       input.push_back(st);
  89.    }
  90.  
  91.    nl = 0, nr = 0;
  92.  
  93.    for (int i = 0; i < h; ++i) {
  94.       for (int j = 0; j < w; ++j) {
  95.          if (input[i][j] == '*') {
  96.             mp[w * i + j] = ((i & 1)^(j & 1) ? nl++ : nr++);
  97.  
  98.             if (i != 0 && input[i - 1][j] == '*') {
  99.                if ((i & 1)^(j & 1)) add_edge (mp[w * i + j], mp[w * (i - 1) + j]);
  100.                else add_edge (mp[w * (i - 1) + j], mp[w * i + j]);
  101.             }
  102.             if (j != 0 && input[i][j - 1] == '*') {
  103.                if ((i & 1)^(j & 1)) add_edge (mp[w * i + j], mp[w * i + j - 1]);
  104.                else add_edge (mp[w * i + j - 1], mp[w * i + j]);
  105.             }
  106.          }
  107.       }
  108.    }
  109.    n = nl + nr;
  110.    re_init();
  111.  
  112.    int MIS = nl + nr - MCBM();
  113.    cout << MIS << '\n'; // Maximum Independent Set
  114. }
  115.  
  116. signed main() {
  117.    IOS; PREC;
  118.    int tc;
  119.    cin >> tc;
  120.    while (tc--) {
  121.       read();
  122.    }
  123.  
  124.    return EXIT_SUCCESS;
  125. }
Add Comment
Please, Sign In to add comment