Advertisement
IanisBelu

RA Duty Scheduler

Oct 7th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | Source Code | 0 0
  1. #pragma GCC optimize("Ofast,unroll-loops")
  2. #include <bits/stdc++.h>
  3.  
  4. #define sz(a) ((int)(a).size())
  5. #define all(a) (a).begin(), (a).end()
  6.  
  7. #define fi first
  8. #define se second
  9.  
  10. using namespace std;
  11.  
  12. #ifndef LOCAL
  13. #define endl '\n'
  14. #endif
  15.  
  16. typedef long long ll;
  17. typedef pair<int, int> pii;
  18.  
  19. const int NMAX = 105;
  20. const int INF = 2e9+5;
  21.  
  22. struct Edge {
  23.     int dest, flow, rev;
  24. };
  25.  
  26. int n, m, n1, n2;
  27. vector<Edge> g[NMAX];
  28. int d[NMAX];
  29. int last[NMAX];
  30. unordered_map<int, string> name;
  31. int ans[NMAX][2];
  32. int ans_cnt[NMAX];
  33.  
  34. void add_edge(int x, int y, int c) {
  35.     g[x].push_back({ y, c, sz(g[y]) });
  36.     g[y].push_back({ x, 0, sz(g[x]) - 1 });
  37. }
  38.  
  39. void io() {
  40. #ifdef LOCAL
  41.     freopen("input.txt", "r", stdin);
  42. #else
  43.     ios_base::sync_with_stdio(false);
  44.     cin.tie(0);
  45.     cout.tie(0);
  46. #endif
  47. }
  48.  
  49. void read() {
  50.     cin >> n1 >> n2;
  51.     n = n1 + n2 + 2;
  52.     string s;
  53.     for (int i = 2, x, cnt; i <= n1 + 1; i++) {
  54.         cin >> s >> cnt;
  55.         name[i] = s;
  56.         while (cnt--) {
  57.             cin >> x;
  58.             add_edge(i, x + n1 + 1, 1);
  59.         }
  60.     }
  61.     for (int i = 2; i <= n1 + 1; i++)
  62.         add_edge(1, i, 3);
  63.     for (int i = n1 + 2; i < n; i++)
  64.         add_edge(i, n, 2);
  65. }
  66.  
  67. bool bfs() {
  68.     memset(d, 0, sizeof(d));
  69.     memset(last, 0, sizeof(last));
  70.  
  71.     queue<int> q;
  72.     q.push(1);
  73.     d[1] = 1;
  74.  
  75.     while (!q.empty()) {
  76.         int u = q.front();
  77.         q.pop();
  78.         if (u == n) continue;
  79.         for (auto &it : g[u]) {
  80.             if (!d[it.dest] && it.flow > 0) {
  81.                 d[it.dest] = d[u] + 1;
  82.                 q.push(it.dest);
  83.             }
  84.         }
  85.     }
  86.  
  87.     return d[n];
  88. }
  89.  
  90. int dfs(int x, int flow) {
  91.     if (x == n) return flow;
  92.  
  93.     for (; last[x] < sz(g[x]); last[x]++) {
  94.         Edge &e = g[x][last[x]];
  95.         if (e.flow > 0 && d[e.dest] == d[x] + 1) {
  96.             if (int ret = dfs(e.dest, min(flow, e.flow))) {
  97.                 e.flow -= ret;
  98.                 g[e.dest][e.rev].flow += ret;
  99.                 return ret;
  100.             }
  101.         }
  102.     }
  103.  
  104.     return 0;
  105. }
  106.  
  107. int dinic(int x) {
  108.     for (int i = 1; i <= n; i++) {
  109.         for (auto &it : g[i]) {
  110.             it.flow = 0;
  111.             if (i == 1)
  112.                 it.flow = x;
  113.             else if (it.dest == n)
  114.                 it.flow = 2;
  115.             else if (i < it.dest)
  116.                 it.flow = 1;
  117.         }
  118.     }
  119.     while (bfs()) {
  120.         while (int flow = dfs(1, INF));
  121.     }
  122.     for (int i = 1; i <= n2; i++) ans_cnt[i] = 0;
  123.     int ret = 0;
  124.     for (int i = 2; i <= n1 + 1; i++) {
  125.         for (auto &it : g[i]) {
  126.             if (it.flow == 0 && it.dest != 1) {
  127.                 int idx = it.dest - n1 - 1;
  128.                 ans[idx][ans_cnt[idx]++] = i;
  129.                 ret++;
  130.             }
  131.         }
  132.     }
  133.     return ret;
  134. }
  135.  
  136. signed main() {
  137.     io();
  138.     read();
  139.     for (int i = 1; ; i++) {
  140.         if (dinic(i) == 2 * n2) {
  141.             cout << i << endl;
  142.             break;
  143.         }
  144.     }
  145.     for (int i = 1; i <= n2; i++)
  146.         cout << "Day " << i << ": " << name[ans[i][0]] << ' ' << name[ans[i][1]] << endl;
  147.     return 0;
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement