mr_dot_convict

12083-Guardian-of-decency-UVa-mr.convict

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