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

#define ll long long
#define ii pair <int,int>
#define F first
#define S second
#define ever (;;)

const int N = 100100;

int n,m,k,freq[N],num,ans,cum[N],mx;
vector <int> vr[N],vl[N],v;

int main()
{
    scanf("%d%d%d",&n,&m,&k);

    for(int i=1;i<=k;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);

        vl[a].push_back(b);
        vr[b].push_back(a);
        freq[a]++;
    }

    for(int i=1;i<=n;i++)
        num += ( freq[i] >= 1 );

    ans = num;

    for(int i=1;i<=m;i++)
    {
        int curr = num+1;

        for(auto &x:vr[i])
            curr -= ( freq[x] == 1 );

        ans = min( curr , ans );
    }

    for(int i=1;i<=m;i++)
    {
        v.clear();
        mx = 0;

        int curr = num+2;

        for(auto &x:vr[i])
        {
            curr -= ( freq[x] == 1 );
            if( freq[x] == 2 )
                v.push_back(x);
        }

        for(auto &x:v)
        {
            for(auto &y:vl[x])
            {
                if( y == i )
                    continue;

                cum[y]++;
                mx = max( mx , cum[y] );
            }
        }

        curr -= mx;

        ans = min( ans , curr );

        for(auto &x:v)
        {
            for(auto &y:vl[x])
            {
                if( y == i )
                    continue;

                cum[y]--;
            }
        }
    }

    printf("%d\n",ans);
}
Copy
Min Vertex Cover 2 Naseem17
GNU G++17
17 ms
5.4 MB
Wrong Answer