Language: C++

#include <bits/stdc++.h>
#define F(i,j,k) for(int i=(j);i<=(k);++i)
using namespace std;
const int N=100005;
struct V{int head;}v[N];
struct E{int u,v,next;}e[N*2];
int n,m,ecnt=1,degree[N];
inline void add(int x,int y){
	e[++ecnt].u=x,e[ecnt].v=y;
	e[ecnt].next=v[x].head,v[x].head=ecnt;
	e[++ecnt].u=y,e[ecnt].v=x;
	e[ecnt].next=v[y].head,v[y].head=ecnt;
	degree[x]++,degree[y]++;
}
#define ll long long
int size[N];
ll ans;
void dfs(int x,int f,int fl){
	size[x]+=fl,vis[x]=1;
	for(int i=v[x].head;i;i=e[i].next){
		int y=e[i].v;
		if(y==f)continue;
		if(!vis[y])dfs(y,x,fl*-1);
		size[x]+=size[y];
	}
	ans+=abs(size[x]);
}
int fa[N];
bool vis[N];
inline int find(int x){return fa[x]=(x==fa[x]?x:find(fa[x]));}
inline void uni(int x,int y){fa[find(x)]=find(y);}
void get(int x){
	for(int i=v[x].head;i;i=e[i].next){
		int y=e[i].v;
		if(!fa[y])fa[y]=x,get(y);
	}
}
int main(){
	//freopen("txt.in","r",stdin);freopen("txt.out","w",stdout);
	scanf("%d %d\n",&n,&m);
	int tu,tv;
	int U,V;
	F(i,1,m){
		scanf("%d %d\n",&tu,&tv),add(tu,tv);
		if(find(tu)==find(tv))U=tu,V=tv;
		else uni(tu,tv);
	}
	if(n==m){
		memset(fa,0,sizeof(fa));
		get(U);
		int top=0,s[N];
		for(int i=V;i;i=fa[i])s[++top]=i;
		if(top&1){
			dfs(U,0,1);
			int temp=size[U];
			if(temp%2){printf("-1\n");return 0;}
			else{
				F(i,1,n)size[U]=0;
				size[U]-=temp/2,size[V]-=temp/2;
				memset(vis,0,sizeof(vis));
				dfs(U,0,1);
				printf("%lld\n",ans+temp/2);
			}
		}
		else{
			
		}
	}
	else{
		F(i,1,n)if(degree[i]==1){
			dfs(i,0,1);
			if(!size[i])printf("%lld\n",ans);
			else printf("-1\n");
			return 0;
		}
	}
	return 0;
} 

Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:19:9: error: reference to 'size' is ambiguous
   19 |         size[x]+=fl,vis[x]=1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:19:21: error: 'vis' was not declared in this scope
   19 |         size[x]+=fl,vis[x]=1;
      |                     ^~~
a.cc:24:17: error: reference to 'size' is ambiguous
   24 |                 size[x]+=size[y];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:24:26: error: reference to 'size' is ambiguous
   24 |                 size[x]+=size[y];
      |                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:26:18: error: reference to 'size' is ambiguous
   26 |         ans+=abs(size[x]);
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:55:34: error: reference to 'size' is ambiguous
   55 |                         int temp=size[U];
      |                                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:58:41: error: reference to 'size' is ambiguous
   58 |                                 F(i,1,n)size[U]=0;
      |                                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:59:33: error: reference to 'size' is ambiguous
   59 |                                 size[U]-=temp/2,size[V]-=temp/2;
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:59:49: error: reference to 'size' is ambiguous
   59 |                                 size[U]-=temp/2,size[V]-=temp/2;
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~
a.cc:72:29: error: reference to 'size' is ambiguous
   72 |                         if(!size[i])printf("%lld\n",ans);
      |                             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:16:5: note:                 'int size [100005]'
   16 | int size[N];
      |     ^~~~