Submission #990219

# Submission time Handle Problem Language Result Execution time Memory
990219 2024-05-29T22:02:47 Z MilosMilutinovic Marriage questions (IZhO14_marriage) C++14
100 / 100
442 ms 2152 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<int>> g(m);
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    --a; --b;
    g[b].push_back(a);
  }
  for (int i = 0; i < m; i++) {
    sort(g[i].begin(), g[i].end());
  }
  vector<int> par(n, -1);
  vector<int> mtc(m, -1);
  vector<int> dfn(m);
  int ptr = -1;
  int T = 0;
  function<bool(int)> Match = [&](int v) {
    if (dfn[v] == T) {
      return false;
    }
    dfn[v] = T;
    for (int u : g[v]) {
      if (u > ptr) {
        continue;
      }
      if (par[u] == -1 || Match(par[u])) {
        par[u] = v;
        mtc[v] = u;
        return true;
      }
    }
    return false;
  };
  long long res = 0;
  vector<int> que;
  for (int i = 0; i < m; i++) {
    que.emplace_back(i);
  }
  for (int i = 0; i < n; i++) {
    while (ptr + 1 < n && !que.empty()) {
      ptr += 1;
      while (!que.empty()) {
        T += 1;
        int v = que.back();
        if (Match(v)) {
          que.pop_back();
        } else {
          break;
        }
      }
    }
    if (que.empty()) {
      res += n - ptr;
    }
    if (par[i] != -1) {
      for (int j = 0; j < m; j++) {
        if (!g[j].empty() && g[j][0] == i) {
          vector<int> new_g;
          for (int k = 1; k < (int) g[j].size(); k++) {
            new_g.push_back(g[j][k]);
          }
          g[j] = new_g;
        }
      }
      T += 1;
      mtc[par[i]] = -1;
      Match(par[i]);
      if (mtc[par[i]] == -1) {
        que.push_back(par[i]);
        mtc[par[i]] = -1;
      }
    }
  }
  cout << res << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 456 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 28 ms 604 KB Output is correct
26 Correct 3 ms 348 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 4 ms 348 KB Output is correct
30 Correct 3 ms 544 KB Output is correct
31 Correct 185 ms 1160 KB Output is correct
32 Correct 8 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 169 ms 1768 KB Output is correct
36 Correct 129 ms 1640 KB Output is correct
37 Correct 442 ms 1288 KB Output is correct
38 Correct 51 ms 1628 KB Output is correct
39 Correct 63 ms 604 KB Output is correct
40 Correct 4 ms 600 KB Output is correct
41 Correct 28 ms 860 KB Output is correct
42 Correct 33 ms 860 KB Output is correct
43 Correct 27 ms 1116 KB Output is correct
44 Correct 47 ms 1908 KB Output is correct
45 Correct 76 ms 1112 KB Output is correct
46 Correct 201 ms 2148 KB Output is correct
47 Correct 79 ms 2152 KB Output is correct
48 Correct 105 ms 1864 KB Output is correct
49 Correct 265 ms 2140 KB Output is correct
50 Correct 185 ms 604 KB Output is correct