Source Code
#include <bits/stdc++.h>
using namespace std;
 
#define S second
#define F first
#define LL long long 
#define LINF 100000000000000000
#define INF 1000000000
 
const int N = 2e5+10;
const LL MOD = 1e9+7;

set<pair<int,int>>st;
vector<int>adj[N];  
set<int>st1[N];
int a[N],anss[N];

int main()
{      
    //freopen("out.txt","w",stdout);

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

    int cnt = 0;
    for(int i=1;i<=m;i++){
        
        int u,v;
        scanf("%d%d",&u,&v);
        swap(u,v);

        if(st.find({u,v})!=st.end())continue;
        st.insert({u,v});

        if(!a[v])cnt++;
        a[v]++;
        adj[u].push_back(v);
        st1[v].insert(u);
    }

    map<pair<int,int>,int>mp;
    for(int i=1;i<=n;i++){
        if(st1[i].size()!=2)continue;
        auto it = st1[i].begin();
        int u = *it,v = *(it++);
        if(u>v)swap(u,v);
        mp[{u,v}]++;
    }

    int ans = 0;
    for(int i=1;i<=k;i++){
        
        for(auto x:adj[i]){
            a[x]--;
            if(!a[x])cnt--,anss[i]++;
        }
        
        ans = max(ans,anss[i]-1);
        
        for(auto x:adj[i]){
            if(!a[x])cnt++;
            a[x]++;
        }
    }

    for(auto x:mp)
        ans = max(ans,x.S+anss[x.F.F]+anss[x.F.S]-2);

    int mx1 = 0,mx2 = 0;
    for(int i=1;i<=k;i++){
        if(anss[i]>mx1)mx2 = mx1,mx1 = anss[i];
        else if(anss[i]>mx2)mx2 = anss[i];
    }

    ans = max(ans,mx1+mx2-2);
    printf("%d\n",cnt-ans);
}
Copy
Min Vertex Cover 2 Hazem17
GNU G++17
214 ms
23.9 MB
Wrong Answer