Advertisement
cosenza987

Untitled

Jan 23rd, 2024
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. template <class T = int>
  8. class Dinic {
  9. public:
  10.     struct Edge {
  11.         Edge(int a, T b) { to = a;cap = b; }
  12.         int to;
  13.         T cap;
  14.     };
  15.  
  16.     Dinic(int _n) : n(_n) {
  17.         edges.resize(n);
  18.     }
  19.  
  20.     T maxFlow(int src, int sink) {
  21.         T ans = 0;
  22.         while (bfs(src, sink)) {
  23.             // maybe random shuffle edges against bad cases?
  24.             T flow;
  25.             pt = std::vector<int>(n, 0);
  26.             while ((flow = dfs(src, sink))) {
  27.                 ans += flow;
  28.             }
  29.         }
  30.         return ans;
  31.     }
  32.  
  33.     void addEdge(int from, int to, T cap, T other = 0) {
  34.         edges[from].push_back(list.size());
  35.         list.push_back(Edge(to, cap));
  36.         edges[to].push_back(list.size());
  37.         list.push_back(Edge(from, other));
  38.     }
  39.  
  40.     bool inCut(int u) const { return h[u] < n; }
  41.     int size() const { return n; }
  42.     int find(int s) {
  43.         int st = s;
  44.         bool ok = true;
  45.         while(ok) {
  46.             ok = false;
  47.             for(auto e : edges[s]) {
  48.                 if(list[e].to == s + 1 and list[e].cap == 0) {
  49.                     s++;
  50.                     ok = true;
  51.                 }
  52.             }
  53.         }
  54.         return s - st;
  55.     }
  56. private:
  57.     int n;
  58.     std::vector<std::vector<int> > edges;
  59.     std::vector<Edge> list;
  60.     std::vector<int> h, pt;
  61.  
  62.     T dfs(int on, int sink, T flow = 1e9) {
  63.         if (flow == 0) {
  64.             return 0;
  65.         } if (on == sink) {
  66.             return flow;
  67.         }
  68.         for (; pt[on] < (int)edges[on].size(); pt[on]++) {
  69.             int cur = edges[on][pt[on]];
  70.             if (h[on] + 1 != h[list[cur].to]) {
  71.                 continue;
  72.             }
  73.             T got = dfs(list[cur].to, sink, std::min(flow, list[cur].cap));
  74.             if (got) {
  75.                 list[cur].cap -= got;
  76.                 list[cur ^ 1].cap += got;
  77.                 return got;
  78.             }
  79.         }
  80.         return 0;
  81.     }
  82.  
  83.     bool bfs(int src, int sink) {
  84.         h = std::vector<int>(n, n);
  85.         h[src] = 0;
  86.         std::queue<int> q;
  87.         q.push(src);
  88.         while (!q.empty()) {
  89.             int on = q.front();
  90.             q.pop();
  91.             for (auto a : edges[on]) {
  92.                 if (list[a].cap == 0) {
  93.                     continue;
  94.                 }
  95.                 int to = list[a].to;
  96.                 if (h[to] > h[on] + 1) {
  97.                     h[to] = h[on] + 1;
  98.                     q.push(to);
  99.                 }
  100.             }
  101.         }
  102.         return h[sink] < n;
  103.     }
  104. };
  105.  
  106. int main() {
  107.     ios_base::sync_with_stdio(false);
  108.     cin.tie(nullptr);
  109.     int n, m;
  110.     cin >> n >> m;
  111.     vector<int> p(n), c(n);
  112.     set<int> s;
  113.     for(int i = 0; i < n; i++) {
  114.         s.insert(i);
  115.     }
  116.     for(int i = 0; i < n; i++) {
  117.         cin >> p[i];
  118.     }
  119.     for(int i = 0; i < n; i++) {
  120.         cin >> c[i];
  121.     }
  122.     int q;
  123.     cin >> q;
  124.     vector<int> qq(q), ans(q);
  125.     for(int i = 0; i < q; i++) {
  126.         cin >> qq[i];
  127.         qq[i]--;
  128.         s.erase(qq[i]);
  129.     }
  130.     int id = 0;
  131.     vector<int> ic(m + 1), in(n + 1), out(n + 1);
  132.     for(int i = 1; i <= m; i++) {
  133.         ic[i] = id++;
  134.     }
  135.     for(int i = 0; i <= n; i++) {
  136.         in[i] = id++;
  137.     }
  138.     for(int i = 0; i <= n; i++) {
  139.         out[i] = id++;
  140.     }
  141.     int src = id++;
  142.     int sink = id++;
  143.     Dinic<int> dinic(sink + 1);
  144.     for(int i = 1; i <= m; i++) {
  145.         dinic.addEdge(src, ic[i], 1);
  146.     }
  147.     for(int i = 0; i < n; i++) {
  148.         dinic.addEdge(out[i], out[i + 1], i + 1);
  149.     }
  150.     for(int i = 0; i <= n; i++) {
  151.         dinic.addEdge(in[i], out[i], 1);
  152.     }
  153.     dinic.addEdge(out[n], sink, n + 1);
  154.     for(auto e : s) {
  155.         dinic.addEdge(ic[c[e]], in[p[e]], 1);
  156.     }
  157.     auto res = dinic;
  158.     int sz = s.size(), curans = 0;
  159.     for(int i = q - 1; i >= 0; i--) {
  160.         while(res.maxFlow(src, out[curans]) == curans) {
  161.             curans++;
  162.         }
  163.         ans[i] = curans;
  164.         dinic.addEdge(ic[c[qq[i]]], in[p[qq[i]]], 1);
  165.         res = dinic;
  166.     }
  167.     for(int i = 0; i < q; i++) {
  168.         cout << ans[i] << "\n";
  169.     }
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement