Source Code
#include "bits/stdc++.h"

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m, k;
  cin >> n >> m >> k;
  vector<pair<int, int>> edges;
  for (int i = 0; i < k; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    edges.emplace_back(a, b);
  }
  vector<int> degree(n);
  for (auto [a, b] : edges) {
    degree[a] += 1;
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (degree[i]) ans += 1;
  }
  vector<vector<int>> gr(m);
  for (auto [a, b] : edges) {
    gr[b].push_back(a);
  }
  int mn = ans;
  for (int i = 0; i < m; ++i) {
    int new_ans = ans + 1;
    for (int v : gr[i]) {
      if (degree[v] == 1) new_ans -= 1;
    }
    if (mn > new_ans) mn = new_ans;
  }
  cout << mn << '\n';
  return 0;
}
Copy
Min Vertex Cover 1 abdulrahman_aj
GNU G++17
97 ms
6.6 MB
Accepted