Language: C++

//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\
 
#include <bits/stdc++.h>
 
#define F first
#define S second
#define _sz(x) (int)x.size()
 
using namespace std ;
using ll = long long ;
using ld = long double ;
using pii = pair <int , int> ;
 
int const N = 1e5 + 20 ;
int n , m ;
vector <int> g[N] ;
 
struct edge {
	int u , v ;
	bool in ;
} e[N] ;
 
vector <int> vec ;
int d , ans , h[N] ;
bool vis[N] ;
 
int dfs (int v) {
	int d = 1 ;
	vis[v] = 1 ;
	for (int i : g[v]) {
		int u = e[i].u ^ e[i].v ^ v ;
		if (!vis[u]) {
			e[i].in = 1 ;
			h[u] = h[v] ^ 1 ;
			d -= dfs(u) ;
		}
	}
 
	vec.push_back(d) ;
	ans += abs(d) ;
 
	return d ;
}
 
int token[N] ;
int want = -1 ;
 
int get (int v , int par = -1) {
	int d = 0 ;
	if (h[v] != want) d ++ ;
	d -= token[v] ;
 
	for (int i : g[v]) {
		int u = e[i].u ^ e[i].v ^ v ;
 
		if (e[i].in && u != par) {
			d += get(u , v) ;
		}
	}
 
	ans += abs(d) ;
	return d ;
}
 
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
	
	cin >> n >> m ;
 
	for (int i = 0 ; i < m ; i ++) {
		cin >> e[i].u >> e[i].v ;
		e[i].u -- , e[i].v -- ;
 
		g[e[i].u].push_back(i) ;
		g[e[i].v].push_back(i) ;
	}
 
	d = dfs(0) ;
 
	if (m == n - 1) {
		if (!d) cout << ans << '\n' ;
		else cout << "-1\n" , 0 ;
		return 0 ;
	}
 
	bool odd_cycle = 0 ;
 
	for (int i = 0 ; i < m ; i ++)
		if (!e[i].in && h[e[i].u] == h[e[i].v]) odd_cycle = 1 ;
 
	if (odd_cycle) {
		if (d & 1) return cout << "-1\n" , 0 ;
 
		d /= 2 ;
 
		for (int i = 0 ; i < m ; i ++)
			if (!e[i].in) want = h[e[i].u] , token[e[i].u] = token[e[i].v] = (h[e[i].u] ? -d : d) ;
 
		for (int i = 0 ; i < n ; i ++)
			if (h[i] == want) token[i] ++ ;
 
		ans = abs(d) ;
		get(0) ;
		cout << ans << '\n' ;
	}
	else {
		if (d) return cout << "-1\n" , 0 ;
 
		assert(0) ;
 
		sort(vec.begin() , vec.end()) ;
 
		int x = vec[(n - 1) / 2] ;
		ans += x * (n - 1) + x ;
 
		cout << ans << '\n' ;
	}
}
//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\

#include <bits/stdc++.h>

#define F first
#define S second
#define _sz(x) (int)x.size()

using namespace std ;
using ll = long long ;
using ld = long double ;
using pii = pair <int , int> ;

int const N = 1e5 + 20 ;
int n , m ;
vector <int> g[N] ;

struct edge {
	int u , v ;
	bool in ;
} e[N] ;

vector <int> vec ;
int d , ans , h[N] ;
bool vis[N] ;

int dfs (int v) {
	int d = 1 ;
	vis[v] = 1 ;
	for (int i : g[v]) {
		int u = e[i].u ^ e[i].v ^ v ;
		if (!vis[u]) {
			e[i].in = 1 ;
			h[u] = h[v] ^ 1 ;
			d -= dfs(u) ;
		}
	}

	vec.push_back(d) ;
	ans += abs(d) ;

	return d ;
}

int token[N] ;
int want = -1 ;

int get (int v , int par = -1) {
	int d = 0 ;
	if (h[v] != want) d ++ ;
	d -= token[v] ;

	for (int i : g[v]) {
		int u = e[i].u ^ e[i].v ^ v ;

		if (e[i].in && u != par) {
			d += get(u , v) ;
		}
	}

	ans += abs(d) ;
	return d ;
}

int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
	
	cin >> n >> m ;

	for (int i = 0 ; i < m ; i ++) {
		cin >> e[i].u >> e[i].v ;
		e[i].u -- , e[i].v -- ;

		g[e[i].u].push_back(i) ;
		g[e[i].v].push_back(i) ;
	}

	d = dfs(0) ;

	if (m == n - 1) {
		if (!d) cout << ans << '\n' ;
		else cout << "-1\n" , 0 ;
		return 0 ;
	}

	bool odd_cycle = 0 ;

	for (int i = 0 ; i < m ; i ++)
		if (!e[i].in && h[e[i].u] == h[e[i].v]) odd_cycle = 1 ;

	if (odd_cycle) {
		if (d & 1) return cout << "-1\n" , 0 ;

		d /= 2 ;

		for (int i = 0 ; i < m ; i ++)
			if (!e[i].in) want = h[e[i].u] , token[e[i].u] = token[e[i].v] = -d ;

		for (int i = 0 ; i < n ; i ++)
			if (h[i] == want) token[i] ++ ;

		ans = abs(d) ;
		get(0) ;
		cout << ans << '\n' ;
	}
	else {
		if (d) return cout << "-1\n" , 0 ;

		assert(0) ;

		sort(vec.begin() , vec.end()) ;

		int x = vec[(n - 1) / 2] ;
		ans += x * (n - 1) + x ;

		cout << ans << '\n' ;
	}
}


Compiler output:
a.cc:132:11: error: redefinition of 'const int N'
  132 | int const N = 1e5 + 20 ;
      |           ^
a.cc:14:11: note: 'const int N' previously defined here
   14 | int const N = 1e5 + 20 ;
      |           ^
a.cc:133:5: error: redefinition of 'int n'
  133 | int n , m ;
      |     ^
a.cc:15:5: note: 'int n' previously declared here
   15 | int n , m ;
      |     ^
a.cc:133:9: error: redefinition of 'int m'
  133 | int n , m ;
      |         ^
a.cc:15:9: note: 'int m' previously declared here
   15 | int n , m ;
      |         ^
a.cc:134:14: error: redefinition of 'std::vector<int> g [100020]'
  134 | vector <int> g[N] ;
      |              ^
a.cc:16:14: note: 'std::vector<int> g [100020]' previously declared here
   16 | vector <int> g[N] ;
      |              ^
a.cc:136:8: error: redefinition of 'struct edge'
  136 | struct edge {
      |        ^~~~
a.cc:18:8: note: previous definition of 'struct edge'
   18 | struct edge {
      |        ^~~~
a.cc:139:3: error: conflicting declaration 'int e [100020]'
  139 | } e[N] ;
      |   ^
a.cc:21:3: note: previous declaration as 'edge e [100020]'
   21 | } e[N] ;
      |   ^
a.cc:141:14: error: redefinition of 'std::vector<int> vec'
  141 | vector <int> vec ;
      |              ^~~
a.cc:23:14: note: 'std::vector<int> vec' previously declared here
   23 | vector <int> vec ;
      |              ^~~
a.cc:142:5: error: redefinition of 'int d'
  142 | int d , ans , h[N] ;
      |     ^
a.cc:24:5: note: 'int d' previously declared here
   24 | int d , ans , h[N] ;
      |     ^
a.cc:142:9: error: redefinition of 'int ans'
  142 | int d , ans , h[N] ;
      |         ^~~
a.cc:24:9: note: 'int ans' previously declared here
   24 | int d , ans , h[N] ;
      |         ^~~
a.cc:142:15: error: redefinition of 'int h [100020]'
  142 | int d , ans , h[N] ;
      |               ^
a.cc:24:15: note: 'int h [100020]' previously declared here
   24 | int d , ans , h[N] ;
      |               ^
a.cc:143:6: error: redefinition of 'bool vis [100020]'
  143 | bool vis[N] ;
      |      ^~~
a.cc:25:6: note: 'bool vis [100020]' previously declared here
   25 | bool vis[N] ;
      |      ^~~
a.cc:145:5: error: redefinition of 'int dfs(int)'
  145 | int dfs (int v) {
      |     ^~~
a.cc:27:5: note: 'int dfs(int)' previously defined here
   27 | int dfs (int v) {
      |     ^~~
a.cc:163:5: error: redefinition of 'int token [100020]'
  163 | int token[N] ;
      |     ^~~~~
a.cc:45:5: note: 'int token [100020]' previously declared here
   45 | int token[N] ;
      |     ^~~~~
a.cc:164:5: error: redefinition of 'int want'
  164 | int want = -1 ;
      |     ^~~~
a.cc:46:5: note: 'int want' previously defined here
   46 | int want = -1 ;
      |     ^~~~
a.cc:166:5: error: redefinition of 'int get(int, int)'
  166 | int get (int v , int par = -1) {
      |     ^~~
a.cc:48:5: note: 'int get(int, int)' previously defined here
   48 | int get (int v , int par = -1) {
      |     ^~~
a.cc:183:5: error: redefinition of 'int main()'
  183 | int main(){
      |     ^~~~
a.cc:65:5: note: 'int main()' previously defined here
   65 | int main(){
      |     ^~~~