mr_dot_convict

12168-Cats-vs-Dogs-UVa-mr.convict

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