Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;

const int MAX_N = 100005;

vector<int> G[MAX_N];
bool flag;
int visit[MAX_N];

void dfs(int u,int k,int& s,int& t)
{
    visit[u] = k;
    if(k > 0){
        s++;
    }else{
        t++;
    }
    rep(i,len(G[u])){
        if(visit[G[u][i]] == 0){
            dfs(G[u][i],-k,s,t);
            if(flag){
                return;
            }
        }else if(visit[G[u][i]] == k){
            flag = true;
            return;
        }
    }
}

map<int,int> mp;
bool dp[MAX_N];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,m){
        int a,b;
        scanf("%d%d",&a,&b);
        G[a-1].pb(b-1),G[b-1].pb(a-1);
    }
    vector<P> vec;
    rep(i,n){
        if(visit[i] == 0){
            int r1 = 0,r2 = 0;
            dfs(i,1,r1,r2);
            if(flag){
                cout << "-1\n";
                return 0;
            }
            vec.pb(P(r1,r2));
        }
    }
    int sm = 0,al = 0;
    rep(i,len(vec)){
        sm += min(vec[i].fi,vec[i].se);
        al += max(vec[i].fi,vec[i].se);
        if(abs(vec[i].fi-vec[i].se) != 0){
            mp[abs(vec[i].fi-vec[i].se)]++;
        }
    }
    dp[0] = true;
    each(it,mp){
        int num = it.se;
        for(int i=1;num>0;i<<=1){
            int mul = min(i,num);
            for(int j=al-sm;j>=it.fi*mul;j--){
                if(dp[j-it.fi*mul]){
                    dp[j] = true;
                }
            }
            num -= mul;
        }
    }
    int mn = INF;
    rep(i,al-sm+1){
        if(dp[i]){
            cmn(mn,max(sm+i,al-i));
        }
    }
    if(n % 2 == 0){
        if(mn <= n/2){
            cout << (ll)n*n/2/2 - m << "\n";
        }else{
            cout << (ll)mn*(n-mn) - m << "\n";
        }
    }else{
        if(mn <= (n+1)/2){
            cout << (ll)(n-1)*(n+1)/2/2 - m << "\n";
        }else{
            cout << (ll)mn*(n-mn) - m << "\n";
        }
    }
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int, int&, int&)':
a.cc:41:5: error: reference to 'visit' is ambiguous
   41 |     visit[u] = k;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:37:5: note:                 'int visit [100005]'
   37 | int visit[MAX_N];
      |     ^~~~~
a.cc:48:12: error: reference to 'visit' is ambiguous
   48 |         if(visit[G[u][i]] == 0){
      |            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:37:5: note:                 'int visit [100005]'
   37 | int visit[MAX_N];
      |     ^~~~~
a.cc:53:18: error: reference to 'visit' is ambiguous
   53 |         }else if(visit[G[u][i]] == k){
      |                  ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:37:5: note:                 'int visit [100005]'
   37 | int visit[MAX_N];
      |     ^~~~~
a.cc: In function 'int main()':
a.cc:74:12: error: reference to 'visit' is ambiguous
   74 |         if(visit[i] == 0){
      |            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:37:5: note:                 'int visit [100005]'
   37 | int visit[MAX_N];
      |     ^~~~~