Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
const int N=2e5;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
int head[N],next[N],to[N],st[N];
int fa[N];inline int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
int ans,sum[N],siz[N];
bool vis[N];
inline int dfs(int k) {
	int t;
	for (int i=head[k];i;i=next[i])
		if (!vis[to[i]]) {
			vis[to[i]]=true;
			t=dfs(to[i]);
			ans+=abs(t);
			siz[k]-=t;
		}
	return siz[k];
}
inline void get(int k) {
	for (int i=head[k];i;i=next[i])
		if (to[i]!=fa[k])
			fa[to[i]]=k,get(to[i]);
}
int main()
{
	int n=gi(),m=gi(),i,tot=0,a,b,A,B,top=0,t,k;
	for (i=1;i<=n;i++) fa[i]=i,siz[i]=1;
	for (i=1;i<=m;i++) {
		a=gi(),b=gi();
		if (find(a)==find(b)) A=a,B=b;
		else {
			fa[fa[a]]=b;
			to[++tot]=b,next[tot]=head[a],head[a]=tot;
			to[++tot]=a,next[tot]=head[b],head[b]=tot;
		}
	}
	if (m==n-1)
		vis[1]=true,printf("%d\n",dfs(1)?-1:ans);
	else {
		for (i=1;i<=n;i++) fa[i]=0;
		get(A);
		for (k=B;k;k=fa[k]) st[++top]=k;
		if (top&1) {
			vis[A]=true;t=dfs(A);
			for (i=1;i<=n;i++) vis[i]=false,siz[i]=1;
			if (t%2) return puts("-1"),0;
			siz[A]-=t/2,siz[B]-=t/2;
			ans=abs(t/2);
			vis[A]=true;dfs(A);
		} else {
			for (i=1;i<=top;i++) vis[st[i]]=true;
			for (i=1;i<=top;i++) sum[i]=sum[i-1]+(i&1?-1:1)*dfs(st[i]);
			sort(sum+1,sum+1+top);
			for (i=1;i<=top;i++) ans+=abs(sum[top/2]-sum[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


Compiler output:
a.cc: In function 'int dfs(int)':
a.cc:29:32: error: reference to 'next' is ambiguous
   29 |         for (int i=head[k];i;i=next[i])
      |                                ^~~~
In file included from /usr/include/c++/14/string:47,
                 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_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:13: note:                 'int next [200000]'
   23 | int head[N],next[N],to[N],st[N];
      |             ^~~~
a.cc: In function 'void get(int)':
a.cc:39:32: error: reference to 'next' is ambiguous
   39 |         for (int i=head[k];i;i=next[i])
      |                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:13: note:                 'int next [200000]'
   23 | int head[N],next[N],to[N],st[N];
      |             ^~~~
a.cc: In function 'int main()':
a.cc:52:37: error: reference to 'next' is ambiguous
   52 |                         to[++tot]=b,next[tot]=head[a],head[a]=tot;
      |                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:13: note:                 'int next [200000]'
   23 | int head[N],next[N],to[N],st[N];
      |             ^~~~
a.cc:53:37: error: reference to 'next' is ambiguous
   53 |                         to[++tot]=a,next[tot]=head[b],head[b]=tot;
      |                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:23:13: note:                 'int next [200000]'
   23 | int head[N],next[N],to[N],st[N];
      |             ^~~~