Source Code
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stdlib.h> 
#include <time.h>
#include <stack>
#include <map>
#include <math.h>
#include <cmath>
#include <string.h>
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
#define all(x) (x).begin() , (x).end()
#define pb push_back 
const int N = 3e5+5 , MX = 1e9+1 ;
const ll MXL=1e18 , MOD = 1e9 + 7 ; 
int a[N];
vector<int> gl[N] , gr[N];
int main(){
//   freopen("input.txt", "r", stdin);
  int n , m , k ,mx=0 , mx2=0 , cnt=0 ;
  cin >> n >> m >> k ;
  for(int i=0,x,y ; i<k ; i++ ){
    scanf("%d%d" , &x , &y );
    gl[--x].pb(--y);
    gr[y].pb(x);
    // cout<<gl[x].size()<< " ";
    if( !(gl[x].size()-1))
      cnt++;
  }
  // puts("");
  for(int i =0 ; i<m ; i++ ){
    int cnt2=0;
    for(auto j : gr[i] ){
      // cout<<gl[j].size()<< " ";
      if( !(gl[j].size()-1) )
        cnt2++;
    }
    if ( mx < cnt2-1 )
      mx=cnt2-1;
    else if ( mx2 < cnt2-1 )
      mx2=cnt2-1;
    // mx=max(mx,cnt2-1);
    // cout<<endl;
  }
  cout<<cnt-mx-mx2<<endl;
  return 0 ; 
}
Copy
Min Vertex Cover 2 Rand()
GNU G++17
20 ms
14.7 MB
Wrong Answer