Language: C++

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 1e-8;

long long int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> M;
	vector<vector<int>>edge(N, vector<int>(N));
	vector<int>z_connected;
	vector<int>z_noconnected;
	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		L--;
		R--;
		edge[L][R] = 1;
		edge[R][L] = 1;
	}
	z_connected.push_back(0);
	for (int i = 1; i < N; i++) {
		if (edge[0][i]) {
			z_connected.push_back(i);
		}
		else {
			z_noconnected.push_back(i);
		}
	}
	vector<int>dis_node(N);
	vector<vector<int>>dis_edge(N);
	for (int i = 0; i < z_connected.size(); i++) {
		for (int j = i + 1; j < z_connected.size(); j++) {
			if (!edge[z_connected[i]][z_connected[j]]) {
				dis_node[z_connected[i]] = 1;
				dis_node[z_connected[j]] = 1;
				dis_edge[z_connected[i]].push_back(z_connected[j]);
				dis_edge[z_connected[j]].push_back(z_connected[i]);
			}
		}
	}
	queue<int>Q;
	int st = 0;
	for (int i = 0; i < N; i++) {
		if (dis_node[i]) {
			st = i;
			break;
		}
	}
	vector<int>dis(N, MOD);
	dis[st] = 0;
	Q.push(st);
	while (!Q.empty()) {
		int cn = Q.front();
		Q.pop();
		for (auto i : dis_edge[cn]) {
			if (dis[i] > dis[cn] + 1) {
				dis[i] = dis[cn] + 1;
				Q.push(i);
			}
		}
	}
	bool imp = false;
	for (int i = 0; i < N; i++) {
		for (auto j : dis_edge[i]) {
			if (dis[i] % 2 == dis[j] % 2) {
				imp = true;
			}
		}
	}
	if (imp) {
		cout << -1 << endl;
		return 0;
	}
	long long int ans = MOD;
	for (int i = 0; i < 2; i++) {
		auto box = z_noconnected;
		for (int j = 0; j < N; j++) {
			if (dis_node[j] && dis[j] % 2 == i) {
				box.push_back(j);
			}
		}
		bool can = true;
		for (int j = 0; j < box.size(); j++) {
		//	cout << box[j] << endl;
			for (int k = j+1; k < box.size(); k++) {
				if (!edge[box[j]][box[k]]) {
					can = false;
				}
			}
		}
		if (can) {
			ans = min(ans, (int)box.size()*(box.size() - 1) / 2 + (N - box.size())*(N - box.size() - 1) / 2);
		}
	}
	
	if (ans == MOD) {
		cout << -1 << endl;
	}
	else {
		cout << ans << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:113:34: error: no matching function for call to 'min(long long int&, long long unsigned int)'
  113 |                         ans = min(ans, (int)box.size()*(box.size() - 1) / 2 + (N - box.size())*(N - box.size() - 1) / 2);
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:113:34: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
  113 |                         ans = min(ans, (int)box.size()*(box.size() - 1) / 2 + (N - box.size())*(N - box.size() - 1) / 2);
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:113:34: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  113 |                         ans = min(ans, (int)box.size()*(box.size() - 1) / 2 + (N - box.size())*(N - box.size() - 1) / 2);
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~