Source Code
#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

int n , m , k;

int cur[N];

vector< int > g[N];
vector< int > g2[N];

vector< int > v;

int frq[N];

int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int l , r , i = 0 ;i < k;i++){
        scanf("%d%d",&l,&r);
        g[r].push_back(l);
        g2[l].push_back(r);
        cur[l]++;
    }
    int a = 0 , b = 0;
    for(int i = 1;i <= n;i++) if(cur[i] > 0) a++;
    int ans = a;
    for(int mx , tmpNode , now , i = 1;i <= m;i++){
        now = 0;
        for(int j = 0 ;j < (int)g[i].size();j++){
            if(cur[g[i][j]] == 1)
                now++;
            if(cur[g[i][j]] == 2){
                tmpNode = g[i][j];
                for(int k = 0 ;k < (int)g2[tmpNode].size();k++){
                    if(g2[tmpNode][k] == i) continue;
                    tmpNode = g2[tmpNode][k];
                    break;
                }
                frq[tmpNode]++;
                v.push_back(tmpNode);
            }
        }
        mx = 0;
        for(int j = 0 ;j < (int)v.size();j++){
            mx = max(mx , frq[v[j]]);
        }
        while((int)v.size() > 0){
            frq[v.back()] = 0;
            v.pop_back();
        }
        now += mx;
        ans = min(ans , a - now + 1);
    }
    cout << ans << endl;

}
Copy
Min Vertex Cover 2 Kilani
GNU G++17
9 ms
5.3 MB
Wrong Answer