criyle/codenet-compile-errors huggingface dataset:
Language: C++

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
	#define D(a) cerr << #a << " = " << a << endl
#else
	#define D(a)
	#define cerr false && cerr
#endif
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define dforsn(i,s,n) for(int i=int(n-1);i>=int(s);i--)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define dforn(i,n) dforsn(i,0,n)
#define forn(i,n) forsn(i,0,n)
#define all(a) a.begin(),a.end()
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
using pii = pair<int,int>;
using vi = vector<int>;
using ll = long long;

const int N = 1e5, K = 2;

int n;
vi adj[N];

int color[N], n_color[K]; 
int s, t;
void paint(int u = 0, int p = -1, int cur_color = 1) {
    color[u] = cur_color;
    n_color[cur_color==1]++;

    int next_color = -1 * cur_color;
    for (int v : adj[u]) if (v != p) {
        if (!color[v]) paint(v, u, next_color);
        else s = u, t = v;
    }
}

int need[N];
int calc_needs(int u = 0, int p = -1) {
    need[u] = color[u];

    for (int v : adj[u]) if (v != p) {
        if ((u == s && v == t) || (u == t && v == s)) continue;

        need[u] += calc_needs(v, u);
    } 

    return need[u];
}

bool in_cycle[N];
bool find_cycle(int u = s, int p = -1) {
    if (u == t) in_cycle[u] = true;

    for (int v : adj[u]) if (v != p) {
        if ((u == s && v == t) || (u == t && v == s)) continue;
        
        if (find_cycle(v, u)) in_cycle[u] = true;
    }

    return in_cycle[u];
}

ll calc_cost() {
    ll cost = 0;
    forn(u, n) cost += abs(need[u]);
    return cost;
}

void no() {
    cout << -1 << endl;
    exit(0);
}

int main() {
	fastio;

    int m; cin >> n >> m;
    forn(_, m) {
        int u, v; cin >> u >> v; u--, v--; 
        adj[u].pb(v), adj[v].pb(u); 
    }

    paint();
    int dif_colors = n_color[1] - n_color[0];

    ll ans;
    if (m == n - 1) { // árbol
        if (dif_colors != 0) no();

        calc_needs();

        ans = calc_cost();
    }
    else if (color[s] == color[t]) { // ciclo impar
        if (dif_colors % 2 == 1) no();

        int need_add = dif_colors / 2;
        color[s] -= need_add, color[t] -= need_add;
        calc_needs();

        ans = abs(need_add) + calc_cost();
    g
    else { // ciclo par
        if (dif_colors != 0) no();

        calc_needs();

        find_cycle();
        vi sorted_needs; sorted_needs.pb(0);
        forn(u, n) if (in_cycle[u] && u != s) sorted_needs.pb(need[u]);
        sorted_needs.pb(0);

        sort(all(sorted_needs)); 
        int opt_move = sorted_needs[si(sorted_needs) / 2];

        forn(u, n) if (in_cycle[u] && u != s) color[u] -= opt_move;
        color[s] += opt_move;
        calc_needs();

        ans = 2 * abs(opt_move) + calc_cost();
    }

    cout << ans << endl;
	
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:109:5: error: 'g' was not declared in this scope
  109 |     g
      |     ^
a.cc:133:2: error: expected '}' at end of input
  133 | }
      |  ^
a.cc:81:12: note: to match this '{'
   81 | int main() {
      |            ^
Language: C++

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef pair <int, int> pii;
#define V first
#define U second
#define ID second

const int N = 1e5 + 5;
int n, m;
int deep[N];
int token[N];
int capa[N];
pii edge[N];
bool in_cycle[N];
bool mark[N];
int id_e = -1;
vector <int> path;
vector <pii> G[N];

void dfs (int v = 0, int par = 0) {
	deep[v] = 1 + deep[par];
	for (pii u: G[v])
		if (u.ID != id_e && u.V != par)
			dfs(u.V, v);
}

void calc (int v = 0, int par = 0) {
	for (pii u: G[v])
		if (u.ID != id_e && u.V != par) {
			calc(u.V, v);
			token[v] += token[u.V];
			capa[v] += capa[u.V];
		}
}

bool find_cycle (int v = 0, int par = 0) {
	mark[v] = true;
	for (pii u: G[v])
		if (!mark[u.V]) {
			path.push_back(u.ID);
			if (find_cycle(u.V, v))
				return true;

			path.pop_back();
		}
		else {
			if (u.V != par){
				id_e = u.ID;
				return true;
			}
		}

	return false;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> edge[i].V >> edge[i].U;
		edge[i].V--, edge[i].U--;

		G[edge[i].V].push_back({edge[i].U, i});
		G[edge[i].U].push_back({edge[i].V, i});
	}

	if (m == n - 1) {
		dfs();
		int mod[2] = {0};
		for (int i = 0; i < n; i++)
			mod[deep[i] & 1]++;
		
		if (mod[0] != mod[1])
			return cout << "-1\n", 0;
		
		for (int i = 0; i < n; i++)
			if (deep[i] & 1)
				token[i]++;
			else
				capa[i]++;

		calc();
		int ans = 0;
		for (int i = 0; i < m; i++) {
			if (deep[edge[i].U] < deep[edge[i].V])
				swap(edge[i].V, edge[i].U);

 			ans += abs(token[edge[i].U] - capa[edge[i].U]);
		}

		return cout << ans << "\n", 0;
	}

	find_cycle();
	int ver = edge[id_e].V;
	path.clear();
	memset(mark, 0, sizeof mark);

	find_cycle(ver);

	if (path.size() & 1) {
		for (int id: path)
			in_cycle[id] = true;
		in_cycle[id_e] = true;

		dfs();
		int mod[2] = {0};
		for (int i = 0; i < n; i++)
			mod[deep[i] & 1]++;

		if (mod[0] != mod[1])
			return cout << "-1\n", 0;

		for (int i = 0; i < n; i++)
			if (deep[i] & 1)
				token[i]++;
			else
				capa[i]++;

		calc();
		vector <int> pp;
		pp.push_back(0);
		int ans = 0;

		for (int i = 0; i < n; i++) {
			if (deep[edge[i].U] < deep[edge[i].V])
				swap(edge[i].V, edge[i].U);

			if (in_cycle[i])
				pp.push_back(token[edge[i].U] - capa[edge[i].U]);
			else
				ans += abs(token[edge[i].U] - capa[edge[i].U]);
		}
		// cout << ans << "\n";
		// for (int id: pp)
		// 	cout << id << " ";
		// cout << "\n";

		sort(pp.begin(), pp.end());
		for (int i = 0; i < pp.size(); i++)
			ans += abs(pp[i] - pp[pp.size() / 2]);
		
		cout << ans << "\n";
		return 0;
	}
	exit(-1);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:100:9: error: 'memset' was not declared in this scope
  100 |         memset(mark, 0, sizeof mark);
      |         ^~~~~~
a.cc:4:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    3 | #include <vector>
  +++ |+#include <cstring>
    4 | using namespace std;
Language: C++

#include<bits/stdc++.h>
using namespace std;
inline int gi()
{
	char c=getchar(); int x=0;
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
	return x;
}
const int N=1e5+5;
vector<int> e[N];
int col[N],f[N],s[N],ban[N],n,m,cu,cv;
void dfs1(int u, int fa)
{
	for(auto v:e[u]) if(v!=fa&&ban[u]!=v)
	{
		if(col[v])
		{
			cu=u,cv=v,ban[u]=v,ban[v]=u;
			continue;
		}
		col[v]=-col[u],dfs1(v,u);
	}
}
void dfs2(int u, int fa)
{
	f[u]=col[u];
	for(auto v:e[u]) if(v!=fa&&ban[u]!=v)
		dfs2(v,u),f[u]+=f[v],s[u]+=s[v];
}
int main()
{
		n=gi(),m=gi();
		for(int i=1;i<=n;++i) e[i].clear(),col[i]=f[i]=ban[i]=s[i]=0;
		for(int i=1;i<=m;++i)
		{
			int u=gi(),v=gi();
			e[u].push_back(v),e[v].push_back(u);
		}
		if(n&1) {puts("-1"); continue;}
		col[1]=1,dfs1(1,0);
		s[cu]=1,s[cv]=-1,dfs2(1,0);
		if(m==n-1)
		{
			if(f[1]) {puts("-1"); continue;}
			int ans=0;
			for(int i=1;i<=n;++i) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
		else if(col[cu]==col[cv])
		{
			if(f[1]&1) {puts("-1"); continue;}
			int ans=abs(f[1]>>1);
			col[cu]-=(f[1]>>1),col[cv]-=(f[1]>>1);
			dfs2(1,0);
			for(int i=1;i<=n;++i) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
		else
		{
			if(f[1]) {puts("-1"); continue;}
			int ans=0;
			vector<int> v;
			for(int i=1;i<=n;++i)
				if(!s[i]) ans+=abs(f[i]);
				else v.push_back(s[i]*f[i]);
			sort(v.begin(),v.end());
			const int mid=v[v.size()-1>>1];
			for(auto x:v) ans+=abs(x-mid);
			printf("%d\n",ans);
		}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:40:38: error: continue statement not within a loop
   40 |                 if(n&1) {puts("-1"); continue;}
      |                                      ^~~~~~~~
a.cc:45:47: error: continue statement not within a loop
   45 |                         if(f[1]) {puts("-1"); continue;}
      |                                               ^~~~~~~~
a.cc:52:49: error: continue statement not within a loop
   52 |                         if(f[1]&1) {puts("-1"); continue;}
      |                                                 ^~~~~~~~
a.cc:61:47: error: continue statement not within a loop
   61 |                         if(f[1]) {puts("-1"); continue;}
      |                                               ^~~~~~~~
Language: C++

#include<bits/stdc++.h>
using namespace std;
inline int gi()
{
	char c=getchar(); int x=0;
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
	return x;
}
const int N=1e5+5;
vector<int> e[N];
int col[N],f[N],ban[N],n,m,cu,cv;
void dfs1(int u, int fa)
{
	for(auto v:e[u]) if(v!=fa&&ban[u]!=v)
	{
		if(col[v])
		{
			cu=u,cv=v,ban[u]=v,ban[v]=u;
			continue;
		}
		col[v]=-col[u],dfs1(v,u);
	}
}
void dfs2(int u, int fa)
{
	f[u]=col[u];
	for(auto v:e[u]) if(v!=fa&&ban[u]!=v)
		dfs2(v,u),f[u]+=f[v];
}
int main()
{
		n=gi(),m=gi();
		for(int i=1;i<=n;++i) e[i].clear(),col[i]=f[i]=ban[i]=0;
		for(int i=1;i<=m;++i)
		{
			int u=gi(),v=gi();
			e[u].push_back(v),e[v].push_back(u);
		}
		if(n&1) {puts("-1"); continue;}
		col[1]=1,dfs1(1,0),dfs2(1,0);
		if(m==n-1)
		{
			if(f[1]) {puts("-1"); continue;}
			int ans=0;
			for(int i=1;i<=n;++i) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
		else
		{
			if(f[1]&1) {puts("-1"); continue;}
			int ans=abs(f[1]>>1);
			col[cu]-=(f[1]>>1),col[cv]-=(f[1]>>1);
			dfs2(1,0);
			for(int i=1;i<=n;++i) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:40:38: error: continue statement not within a loop
   40 |                 if(n&1) {puts("-1"); continue;}
      |                                      ^~~~~~~~
a.cc:44:47: error: continue statement not within a loop
   44 |                         if(f[1]) {puts("-1"); continue;}
      |                                               ^~~~~~~~
a.cc:51:49: error: continue statement not within a loop
   51 |                         if(f[1]&1) {puts("-1"); continue;}
      |                                                 ^~~~~~~~
Language: C++

#include<cstdio>
using namespace std;
const int N=1e5+5;
struct Link{int to,nxt;}l[N<<1];
int n,m,a[N],head[N],tot=1;
int fa[N],edge,rt;
inline int read(){
	int s=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') f=ch=='-'?-1:f,ch=getchar();
	while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
	return s*f;
}
inline void add(int x,int y){
	l[++tot].to=y;l[tot].nxt=head[x];head[x]=tot;
}
int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
void dfs(int x,int fa,int opt){
	a[x]=opt;
	for(int i=head[x];i;i=l[i].nxt){
		int y=l[i].to;
		if(y==fa) continue;
		dfs(y,x);a[x]+=a[y];
	}
}
int st[N],in[N],top;
void get_roof(int x,int fa){
	in[st[++top]=x]=1;
	for(int i=head[x];i;i=l[i].nxt){
		int y=l[i].to;
		if(y==fa) continue;
		if(in[y]){
			edge=i;rt=x;
			do{
				
			}while()
		}
		else get_roof(y,x);
	}
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;++i){
		int x=read(),y=read();
		add(x,y),add(y,x);
	}
	dfs(rt,0);
	if(n==m){
		if()
	}
	else if(a[rt]) return puts("-1"),0;
	int res=0;
	for(int i=1;i<=n;++i) res+=abs(a[i]);
}

Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:25:20: error: too few arguments to function 'void dfs(int, int, int)'
   25 |                 dfs(y,x);a[x]+=a[y];
      |                 ~~~^~~~~
a.cc:20:6: note: declared here
   20 | void dfs(int x,int fa,int opt){
      |      ^~~
a.cc: In function 'void get_roof(int, int)':
a.cc:38:32: error: expected primary-expression before ')' token
   38 |                         }while()
      |                                ^
a.cc:38:33: error: expected ';' before '}' token
   38 |                         }while()
      |                                 ^
      |                                 ;
   39 |                 }
      |                 ~                
a.cc: In function 'int main()':
a.cc:49:12: error: too few arguments to function 'void dfs(int, int, int)'
   49 |         dfs(rt,0);
      |         ~~~^~~~~~
a.cc:20:6: note: declared here
   20 | void dfs(int x,int fa,int opt){
      |      ^~~
a.cc:51:20: error: expected primary-expression before ')' token
   51 |                 if()
      |                    ^
a.cc:52:9: error: expected primary-expression before '}' token
   52 |         }
      |         ^
a.cc:55:36: error: 'abs' was not declared in this scope
   55 |         for(int i=1;i<=n;++i) res+=abs(a[i]);
      |                                    ^~~
Language: C++

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int h[100011];
int d[100011];
int can[100011];
vector<int>tab[100011];
ll ans=0;
int maxHNode=0;
int maxH=0;
void dfs1(int now,int fa)
{
	h[now]=0;
	int sz=tab[now].size();
	int mx=0,lmx=0;
	for(int i=0;i<sz;++i)
	{
		int nex=tab[now][i];
		if(nex==fa)continue;
		dfs1(nex,now);
		if(h[nex]>=mx)
		{
			lmx=mx;
			mx=h[nex];
		}else if(h[nex]>=lmx)
		{
			lmx=h[nex];
		}
	}
	h[now]=lmx+1;
	//cout<<now<<" "<<h[now]<<endl;
	if(h[now]>maxH)
	{
		maxH=h[now];
		maxHNode=now;
	}
}
void dfs2(int now,int fa,int depth)
{
	d[now]=depth;
	can[h[now]]+=(d[now]&1)?-1:1;
	ans+=d[now]*((h[now]&1)?1:-1);
	//cout<<now<<" "<<d[now]<<" "<<h[now]<<endl;
	int sz=tab[now].size();
	for(int i=0;i<sz;++i)
	{
		int nex=tab[now][i];
		if(nex==fa)continue;
		dfs2(nex,now,depth+1);
	}
}
int main()
{
	int n,m;
	cin>>n>>m;
	if(m!=n-1)
	{
		cout<<-1<<endl;
		return 0;
	}
	for(int i=1;i<n;++i)
	{
		int a,b;
		cin>>a>>b;
		tab[a].push_back(b);
		tab[b].push_back(a);
	}
	dfs1(1,1);
	dfs2(maxHNode,maxHNode,0);
	for(int i=1;i<=n;++i)
	{
		//cout<<can[i]<<" ";
		if(can[i]!=0)
		{
			cout<<-1<<endl;
			return 0;
		}
	}
	//cout<<endl;
	cout<<ans<<endl; 
	return 0;
}

Compiler output:
a.cc:11:1: error: 'vector' does not name a type
   11 | vector<int>tab[100011];
      | ^~~~~~
a.cc: In function 'void dfs1(int, int)':
a.cc:18:16: error: 'tab' was not declared in this scope; did you mean 'tan'?
   18 |         int sz=tab[now].size();
      |                ^~~
      |                tan
a.cc: In function 'void dfs2(int, int, int)':
a.cc:48:16: error: 'tab' was not declared in this scope; did you mean 'tan'?
   48 |         int sz=tab[now].size();
      |                ^~~
      |                tan
a.cc: In function 'int main()':
a.cc:69:17: error: 'tab' was not declared in this scope; did you mean 'tan'?
   69 |                 tab[a].push_back(b);
      |                 ^~~
      |                 tan
Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s"in", "r", stdin), freopen(s"out", "w", stdout)

template<typename T>
inline void read(T &x) {
	char ch, t = 0; x = 0;
	while(!isdigit(ch = ge)) t |= ch == '-';
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge;
	x = t ? -x : x;
}
inline int read_int() { int x; return read(x), x; }
inline ll read_ll() { ll x; return read(x), x; }

template<typename T>
inline void chkmin(T&a, T b) { a = a < b ? a : b; }

const int MAXN = 100010;

int n;
int m;
int tot;
int fi[MAXN];
int to[MAXN << 1];
int ne[MAXN << 1];

inline void Link(int u, int v) {
	tot++;
	to[tot] = v;
	ne[tot] = fi[u];
	fi[u] = tot;
}

int col[MAXN];

inline ll Dfs(int x, int la, int d) {
	ll res = 0;
	col[x] = d;
	for(int i = fi[x]; i; i = ne[i]) {
		int u = to[i];
		if(u == la) continue;
		res += Dfs1(u, x, -d), col[x] += col[u];
	} return res + abs(col[x]);
}

int main() {
	n = ri, m = ri;

	if(n & 1) return puts("-1"), 0;

	for(int i = 1; i <= m; i++) {
		int u = ri, v = ri;
		Link(u, v), Link(v, u);
	}

	if(m == n - 1) return cout << Dfs(1, 0, 1) << endl, 0;

	return 0;
}

Compiler output:
a.cc: In function 'll Dfs(int, int, int)':
a.cc:52:24: error: 'Dfs1' was not declared in this scope; did you mean 'Dfs'?
   52 |                 res += Dfs1(u, x, -d), col[x] += col[u];
      |                        ^~~~
      |                        Dfs
Language: C++

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
vector<int> v[100005];
LL ans;
int dep[100005];
int val[100005];
int a,b;
int dfs(int x,int f){
    dep[x]=dep[f]+1;
    int sum=0;
    for(auto it:v[x]){
        if(it!=f){
            if((it==a&&x==b)||(it==b&&x==a))continue;
            sum+=dfs(it,x);
        }
    }
    if(dep[x]&1)sum++;
    else sum--;
    sum+=val[x];
    ans+=abs(sum);
    return sum;
}
int vis[100005];
vector<int> circle;
vector<int> stk;
int Find;
void dfsc(int x,int f){
    stk.pb(x);
    vis[x]=1;
    for(auto it:v[x]){
        if(it!=f){
            if(vis[it]){
                while(stk.back()!=it){
                    circle.pb(stk.back());
                    stk.pop_back();
                }
                circle.pb(stk.back());
                Find=1;
            }
            else{
                dfsc(it,x);
            }
            if(Find)return;
        }
    }
    if(stk.back()==x)stk.pop_back();
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i = 0;i<m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        v[x].pb(y);
        v[y].pb(x);
    }
    if(m==n){
        dfsc(1,0);
        for(auto it:circle)
        printf("%d ",it);
        printf("\n");
        if(circle.size()&1){
            a=cirlce[0],b=cirlce[1];
            int x=dfs(1,0);
            if(x&1){
                printf("-1\n");
            }
            else{
                val[circle[0]]=-x/2;
                val[circle[1]]=-x/2;
            }
            printf("%d\n",dfs(1,0));
        }
        else{
            assert(0);
        }
        return 0;
    }
    if(dfs(1,0)){
        printf("-1\n");
    }
    else
    printf("%lld\n",ans);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:84:15: error: 'cirlce' was not declared in this scope; did you mean 'circle'?
   84 |             a=cirlce[0],b=cirlce[1];
      |               ^~~~~~
      |               circle
Language: C++

#include <bits/stdc++.h>
const int N = 1e5 + 54;
int n, m;
namespace Tree {
	struct node { int u, v; } edge[N << 1];
	int head[N], v[N], col[N], cnt;
	long long sum, ans;
	inline void add(int x, int y) { edge[cnt].u = head[x], edge[cnt].v = y, head[x] = cnt++; }
	void dfs(int x, int fa) {
		int i;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa) continue;
			col[y] = 1 - col[x], dfs(y, x);
		}
	}
	void solve(int x, int fa) {
		int i;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa) continue;
			solve(y, x);
			v[x] += v[y];
		}
	}
	void SOLVE() {
		int x, y, i;
		memset(head, -1, sizeof head);
		for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
		col[1] = 1, dfs(1, 0);
		for (i = 1; i <= n; ++i) {
			if (col[i]) v[i] = 1;
			else v[i] = -1;
			sum += v[i];
		}
		if (sum) {
			puts("-1");
			return;
		}
		solve(1, 0);
		for (i = 1; i <= n; ++i) ans += abs(v[i]);
		printf("%lld\n", ans);
	}
}

namespace BRT {
	struct Edge { int id, x, y; } E;
	struct node { int u, v; } edge[N << 1];
	int head[N], v[N], col[N], cnt, tot, p[N], vis[N], f[N];
	long long sum, ans;
	inline void add(int x, int y) { edge[cnt].u = head[x], edge[cnt].v = y, head[x] = cnt++; }
	void dfs(int x, int fa) {
		int i;
		vis[x] = 1;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa) continue;
			if (vis[y]) E.id = i, E.x = x, E.y = y;
			else col[y] = 1 - col[x], dfs(y, x);
		}
	}
	void solve(int x, int fa) {
		int i;
		for (i = head[x]; ~i; i = edge[i].u) {
			int y = edge[i].v;
			if (y == fa || i == E.id || (i ^ 1) == E.id) continue;
			solve(y, x), v[x] += v[y], f[x] += f[y];
		}
	}
	void SOLVE() {
		memset(head, -1, sizeof head);
		int x, y, i;
		for (i = 1; i <= m; ++i) scanf("%d%d", &x, &y), add(x, y), add(y, x);
		col[1] = 1, dfs(1, 0);
		for (i = 1; i <= n; ++i) {
			if (col[i]) v[i] = 1;
			else v[i] = -1;
			sum += v[i];
		}
		if (col[E.x] == col[E.y]) {
			if (abs(sum) & 1) {
				puts("-1");
				return;
			}
			v[E.x] -= sum / 2, v[E.y] -= sum / 2, ans += abs(sum / 2);
		} else {
			if (sum) {
				puts("-1");
				return;
			}
			f[E.x] = 1, f[E.y] = -1;
		}
		solve(1, 0);
		if (col[E.x] == col[E.y]) {
			for (i = 1; i <= n; ++i) ans += abs(v[i]);
			printf("%lld\n", ans);
			return;
		}
		for (i = 1; i <= n; ++i)
			if (f[i] == 0) ans += abs(v[i]);
			else p[++tot] = -v[i];
		p[++tot] = 0;
		std::sort(p + 1, p + tot + 1);
		ll mid = p[(tot + 1) >> 1];
		for (i = 1; i <= tot; ++i) ans += abs(v[i] - mid);
		printf("%lld\n", ans);
	}
}
int main() {
	scanf("%d%d", &n, &m);
	if (m == n - 1) Tree::SOLVE();
	else BRT::SOLVE();
}


Compiler output:
a.cc: In function 'void BRT::SOLVE()':
a.cc:104:17: error: 'll' was not declared in this scope
  104 |                 ll mid = p[(tot + 1) >> 1];
      |                 ^~
a.cc:105:62: error: 'mid' was not declared in this scope
  105 |                 for (i = 1; i <= tot; ++i) ans += abs(v[i] - mid);
      |                                                              ^~~
Language: C++

//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
const int N=1e5+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
int d[N],n,m,res;
void dfs(int u,int fa){
	go(u)if(v!=fa){
		dfs(v,u);
		if(!d[v])d[u]^=1,d[v]^=1,++res;
	}
}
void solve1(){
	for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v),add(v,u);
	dfs(1,0);
	res=(d[1]?res:-1);
	printf("%d\n",res);
}
int main(){
	scanf("%d%d",&n,&m);
	assert(m==n-1);
	return m==n-1?solve1():solve2(),0;
}

Compiler output:
a.cc:12:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 | inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
      |                       ^
a.cc:12:31: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 | inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
      |                               ^
a.cc: In function 'void solve1()':
a.cc:21:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v),add(v,u);
      |                   ^
a.cc:21:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v),add(v,u);
      |                       ^
a.cc:21:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v),add(v,u);
      |                         ^
a.cc: In function 'int main()':
a.cc:29:32: error: 'solve2' was not declared in this scope; did you mean 'solve1'?
   29 |         return m==n-1?solve1():solve2(),0;
      |                                ^~~~~~
      |                                solve1
Language: C++

//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
const int N=1e5+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
int d[N],n,m,res;
void dfs(int u,int fa){
	go(u)if(v!=fa){
		dfs(v,u);
		if(!d[v])d[u]^=1,++res;
	}
}
void solve1(){
	for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v);
	dfs(1,0);
	res=(!d[1]?-1:res);
	printf("%d\n",res);
}
int main(){
	scanf("%d%d",&n,&m);
	assert(m==n-1);
	return m==n-1?solve1():solve2(),0;
}

Compiler output:
a.cc:12:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 | inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
      |                       ^
a.cc:12:31: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 | inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
      |                               ^
a.cc: In function 'void solve1()':
a.cc:21:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v);
      |                   ^
a.cc:21:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v);
      |                       ^
a.cc:21:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(R int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),add(u,v);
      |                         ^
a.cc: In function 'int main()':
a.cc:29:32: error: 'solve2' was not declared in this scope; did you mean 'solve1'?
   29 |         return m==n-1?solve1():solve2(),0;
      |                                ^~~~~~
      |                                solve1
Language: C++

#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

const int MAX_N = 1e5 + 5;
int n, m;
vector <int> G[MAX_N];

bool dfsTree (int v = 0, int p = 0) {
	int size = 1;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;

		G[v].push_back(u);
		G[u].push_back(v);
	}

	if (n & 1) {
		cout << "-1\n";
	}
	assert(false);

	if (n - 1 == m) {
		if (dfsTree())
			cout << (n >> 1) << "\n";
		else
			cout << "-1\n";
	}
	return 0;
}

Compiler output:
a.cc: In function 'bool dfsTree(int, int)':
a.cc:13:1: warning: no return statement in function returning non-void [-Wreturn-type]
   13 | }
      | ^
a.cc: In function 'int main()':
a.cc:30:9: error: 'assert' was not declared in this scope
   30 |         assert(false);
      |         ^~~~~~
a.cc:5:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    4 | #include <vector>
  +++ |+#include <cassert>
    5 | using namespace std;
Language: C++

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
#define pb push_back
using namespace std;
const int maxn=1e5+10;
int f[maxn],vis[maxn],fa[maxn],d[maxn],u,v,x,y,a[maxn],cnt,n,m;
vector<int> G[maxn];ll ans;
void dfs(int u){
	f[u]=1;vis[u]=1;
	for(int v:G[u]) if(v^fa[u]){
		if(vis[v]){x=u,y=v;continue;}
		fa[v]=u;d[v]=d[u]+1;dfs(v);
		f[u]-=f[v];
	}
}
int lca(int u,int v){
	if(d[u]<d[v]) swap(u,v);
	while(d[u]^d[v]) u=fa[u];
	while(u^v) u=fa[u],v=fa[v];
	return u;
}
int main(){
	scanf("%d%d",&n,&m);
	rep(i,1,m) scanf("%d%d",&u,&v),G[u].pb(v),G[v].pb(u);
	dfs(1);
	if(m==n-1){
		if(f[1]) return puts("-1"),0;
		rep(i,1,n) ans+=abs(f[i]);
	}
	else if(!((d[x]^d[y])&1)){
		if(f[1]&1) return puts("-1"),0;
		int fxk=-(f[1]>>1);ans+=abs(fxk);
		for(int u=x;u;u=fa[u]) f[u]+=fxk;
		for(int u=y;u;u=fa[u]) f[u]+=fxk;
		rep(i,1,n) ans+=abs(f[i]);
	}
	else{
		if(f[1]) return puts("-1"),0;
		int t=lca(x,y);
		for(int u=x;u^t;u=fa[u]) a[++cnt]=f[u],vis[u]=0;
		for(int u=y;u^t;u=fa[u]) a[++cnt]=-f[u],vis[u]=0;a[++cnt]=0;
		sort(a+1,a+cnt+1);int fxk=a[cnt+1>>1];
		rep(i,1,n) if(vis[i]) ans+=abs(f[i]);
		rep(i,1,cnt) ans+=abs(a[i]-fxk);
	}
	printf("%lld\n",ans);
	return 0;
}

Compiler output:
a.cc:17:21: error: 'll' does not name a type
   17 | vector<int> G[maxn];ll ans;
      |                     ^~
a.cc: In function 'int main()':
a.cc:38:28: error: 'ans' was not declared in this scope; did you mean 'abs'?
   38 |                 rep(i,1,n) ans+=abs(f[i]);
      |                            ^~~
      |                            abs
a.cc:42:36: error: 'ans' was not declared in this scope; did you mean 'abs'?
   42 |                 int fxk=-(f[1]>>1);ans+=abs(fxk);
      |                                    ^~~
      |                                    abs
a.cc:53:39: error: 'ans' was not declared in this scope; did you mean 'abs'?
   53 |                 rep(i,1,n) if(vis[i]) ans+=abs(f[i]);
      |                                       ^~~
      |                                       abs
a.cc:54:30: error: 'ans' was not declared in this scope; did you mean 'abs'?
   54 |                 rep(i,1,cnt) ans+=abs(a[i]-fxk);
      |                              ^~~
      |                              abs
a.cc:56:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
   56 |         printf("%lld\n",ans);
      |                         ^~~
      |                         abs
Language: C++

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
int h[maxn],go[maxn*2],nxt[maxn*2],fa[maxn],co[maxn],kk[maxn],bb[maxn],sta[maxn];
vector<int>G[maxn]; 
bool bz[maxn];
int i,j,k,l,t,n,m,u,v,a,b,x,tot,top,num;
ll ans;
bool czy,gjx;
void add(int x,int y){
    go[++tot]=y;
    nxt[tot]=h[x];
    h[x]=tot;
}
int getfa(int x){
    if (!fa[x]) return x;
    int t=getfa(fa[x]);
    co[x]=co[x]^co[fa[x]];
    return fa[x]=t;
}
void travel(int x,int y){
    bz[x]=1;
    int t=h[x];
    for(int i=0;i<(int)G[x].size();i++){
    	go[t]=xx;
        if (go[t]!=y){
            if (bz[go[t]]){
                u=x;v=go[t];
                if (co[u]==co[v]) gjx=1;
            }
            else{
                co[go[t]]=co[x]^1;
                travel(go[t],x);
            }
        }
        t=nxt[t];
    }
}
void dfs(int x,int y){
    int t=h[x];
    for(int i=0;i<(int)G[x].size();i++){
    	go[t]=xx;
        if (go[t]!=y&&!(x==u&&go[t]==v||x==v&&go[t]==u)){
            dfs(go[t],x);
            kk[x]+=kk[go[t]];
            bb[x]+=bb[go[t]];
        }
        t=nxt[t];
    }
}
int main(){
    scanf("%d%d",&n,&m);
    fo(i,1,m){
        scanf("%d%d",&j,&k);
        G[j].push_back(k);
        G[k].push_back(j);
        /*a=getfa(j);b=getfa(k);
        if (a!=b){
            add(j,k);add(k,j);
            fa[a]=b;
            co[a]=co[j]^co[k]^1;
        }
        else{
            u=j;v=k;
            if (co[j]==co[k]) gjx=1;
        }*/
    }
    //fo(i,1,n) getfa(i);
    /*if (!co[1])
        fo(i,1,n) co[i]^=1;*/
    co[1]=1;
    travel(1,0);
    fo(i,1,n) 
        if (co[i]) bb[i]=1;else bb[i]=-1;
    fo(i,1,n) num+=bb[i];
    if (m==n-1){
        if (num){
            printf("-1\n");
            return 0;
        }
    }
    else{
        if (gjx){
            if (num%2==1){
                printf("-1\n");
                return 0;
            }
            bb[u]-=num/2;bb[v]-=num/2;
            ans+=(ll)abs(num/2);
        }
        else{
            if (num){
                printf("-1\n");
                return 0;
            }
            kk[u]=1;kk[v]=-1;
        }
    }
    dfs(1,0);
    fo(i,1,n)
        if (!kk[i]) ans+=(ll)abs(bb[i]);
        else{
            if (kk[i]) bb[i]=-bb[i];
            sta[++top]=bb[i];
        }
    sta[++top]=0;
    sort(sta+1,sta+top+1);
    x=sta[(top+1)/2];
    fo(i,1,top) ans+=(ll)abs(sta[i]-x);
    printf("%lld\n",ans);
}

Compiler output:
a.cc: In function 'void travel(int, int)':
a.cc:30:15: error: 'xx' was not declared in this scope; did you mean 'x'?
   30 |         go[t]=xx;
      |               ^~
      |               x
a.cc: In function 'void dfs(int, int)':
a.cc:47:15: error: 'xx' was not declared in this scope; did you mean 'x'?
   47 |         go[t]=xx;
      |               ^~
      |               x
Language: C++

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define DCSB {printf("-1");exit(0);}
using namespace std;
vector<int>G[100010];
struct edge{
    int v,next;
}a[500001];
int n,m,u,v,ans=0,els,huanu,huanv,tot=0,q[500001],head[500001],fa[500001],dep[500001],f[500001];
void add(int u,int v){
    a[++tot].v=v;
    a[tot].next=head[u];
    head[u]=tot;
}
void dfs(int u,int ff,int dpt){
    dep[u]=dpt;
    fa[u]=ff;
    f[u]=(dpt%2)?1:-1;
    for(int i=0;i<(int)G[u].size();i++){
        int v=G[u][i];
        if(v!=ff){
            if(dep[v]){
                huanu=u;
                huanv=v;
                continue;
            }
            dfs(v,u,dpt+1);
            f[u]+=f[v];
        }
    }
}
void gao_tree(){
    if(f[1]!=0)DCSB
    for(int i=1;i<=n;i++)ans+=abs(f[i]);
    printf("%d",ans);
}
void gao_jihuan(){
    if(f[1]%2!=0)DCSB
    els=-f[1]/2;
    ans=abs(els);
    for(;huanu;huanu=fa[huanu])f[huanu]+=els;
    for(;huanv;huanv=fa[huanv])f[huanv]+=els;
    for(int i=1;i<=n;i++)ans+=abs(f[i]);
    printf("%d",ans);
}
void gao_ouhuan(){
    int top,mid;
    top=0;
    if(f[1])DCSB
    for(;huanv!=huanu;huanv=fa[huanv]){
        q[++top]=f[huanv];
        dep[huanv]=0;
    }
    sort(q+1,q+top+1);
    mid=q[(top+1)/2];
    ans=abs(mid);
    for(int i=1;i<=n;i++)if(dep[i])ans+=abs(f[i]);
    for(int i=1;i<=top;i++)ans+=abs(q[i]-mid);
    printf("%d",ans);
}
int main(){
    memset(head,-1,sizeof(head));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0,1);
    if(m==n-1)gao_tree();
    else if((dep[huanv]-dep[huanu])%2==0)gao_jihuan();
    else gao_ouhuan();
    return 0;
}

Compiler output:
a.cc:8:1: error: 'vector' does not name a type
    8 | vector<int>G[100010];
      | ^~~~~~
a.cc: In function 'void dfs(int, int, int)':
a.cc:22:24: error: 'G' was not declared in this scope
   22 |     for(int i=0;i<(int)G[u].size();i++){
      |                        ^
a.cc: In function 'int main()':
a.cc:69:9: error: 'G' was not declared in this scope
   69 |         G[u].push_back(v);
      |         ^
Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100005;

int N,M;
vector<int> adj[MAXN];
int dep[MAXN],sum[MAXN],k[MAXN];
bool vis[MAXN];
int cU,cV,clen;
vector<int> num;

void dfs(int u,int fa=0)
{
	vis[u]=true;
	for(auto v:adj[u])
	{
		if(v==fa)
			continue;
		if(vis[v])
		{
			cU=u;
			cV=v;
			clen=dep[u]-dep[v]+1;
			continue;
		}
		dep[v]=dep[u]+1;
		dfs(v,u);
	}
}
void Calc(int u,int fa=0)
{
	vis[u]=true;
	for(auto v:adj[u])
	{
		if(v==fa||vis[v])
			continue;
		Calc(v,u);
		sum[u]+=sum[v];
		k[u]+=k[v];
	}
}

int main()
{
	scanf("%d%d",&N,&M);
	for(int i=1;i<=M;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1);
	int val=0;
	long long ans=0;
	for(int i=1;i<=N;i++)
	{
		sum[i]=(dep[i]&1)?-1:1;
		val+=sum[i];
	}
	if(M==N-1)
	{
		if(val!=0)
		{
			puts("-1");
			return 0;
		}
	}
	else
	{
		if(clen&1)
		{
			if(val&1)
			{
				puts("-1");
				return 0;
			}
			else
			{
				ans+=(abs(val)/2);
				sum[cU]-=val/2;
				sum[cV]-=val/2;
			}
		}
		else
		{
			if(val!=0)
			{
				puts("-1");
				return 0;
			}
			else
			{
				k[cU]=1;
				k[cU]=-1;
			}
		}
	}
	memset(vis,0,sizeof vis);
	Calc(1);
	for(int i=1;i<=N;i++)
	{
		if(k[i]==0)
			ans+=abs(sum[i]);
		else
		{
			if(k[i]==1)
				sum[i]=-sum[i];
			num.push_back(sum[i]);
		}
	}
	num.push_back(0);
	sort(num.begin(),num.end());
	int x=num[num.size()/2];
	for(auto s:num)
		ans+=1LL*abs(s-x);
	printf("%lld\n",ans);
	
	return 0;
}


Compiler output:
a.cc:8:1: error: 'vector' does not name a type
    8 | vector<int> adj[MAXN];
      | ^~~~~~
a.cc:12:1: error: 'vector' does not name a type
   12 | vector<int> num;
      | ^~~~~~
a.cc: In function 'void dfs(int, int)':
a.cc:17:20: error: 'adj' was not declared in this scope
   17 |         for(auto v:adj[u])
      |                    ^~~
a.cc: In function 'void Calc(int, int)':
a.cc:35:20: error: 'adj' was not declared in this scope
   35 |         for(auto v:adj[u])
      |                    ^~~
a.cc: In function 'int main()':
a.cc:52:17: error: 'adj' was not declared in this scope
   52 |                 adj[a].push_back(b);
      |                 ^~~
a.cc:111:25: error: 'num' was not declared in this scope; did you mean 'sum'?
  111 |                         num.push_back(sum[i]);
      |                         ^~~
      |                         sum
a.cc:114:9: error: 'num' was not declared in this scope; did you mean 'sum'?
  114 |         num.push_back(0);
      |         ^~~
      |         sum
Language: C++

#include<iostream>
#include<cstring>
#include<cstdio>
#define MN 100000
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,head[MN+5],cnt=1,ans=0,f[MN+5],g[MN+5][2],Fa[MN+5],dep[MN+5],A,B,C,c[MN+5],num,mn=1e9,q[MN+5],top;
struct edge{int to,next;}e[MN*2+5];
inline void ins(int f,int t)
{
    e[++cnt]=(edge){t,head[f]};head[f]=cnt;
    e[++cnt]=(edge){f,head[t]};head[t]=cnt;
}
inline int Abs(int x){return x<0?-x:x;}
void dfs(int x,int fa=0)
{
    ++g[x][f[x]];
    for(int i=head[x];i;i=e[i].next)
        if(e[i].to!=fa)
        {
            if(x==A&&e[i].to==B) continue;
            if(x==B&&e[i].to==A) continue;
            f[e[i].to]=f[x]^1;dfs(e[i].to,x);
            g[x][0]+=g[e[i].to][0];
            g[x][1]+=g[e[i].to][1];
        }
    ans+=Abs(g[x][0]-g[x][1]);
}
void FindCircle(int x,int fa)
{
    Fa[x]=fa;
    for(int i=head[x];i;i=e[i].next)
        if(e[i].to!=fa)
        {
            if(dep[e[i].to]) A=x,B=e[i].to;
            else dep[e[i].to]=dep[x]+1,FindCircle(e[i].to,x);
        }
}
int main()
{
    n=read();m=read();if(n&1) return 0*puts("-1");
    if(m<n)
    {
        for(int i=1;i<n;++i) ins(read(),read());
        dfs(1);
        printf("%d\n",g[1][0]==g[1][1]?ans:-1);
    }
    else
    {
        for(int i=1;i<=n;++i) ins(read(),read());
        FindCircle(dep[1]=1,0);
        for(int x=A,y=B;x!=y;C=x=Fa[x])
            if(dep[x]<dep[y]) swap(x,y);
        for(int i=A;i!=C;i=Fa[i]) c[++num]=i;
        c[++num]=C;int len=dep[A]+dep[B]-2*dep[C]+1;num=len;
        for(int i=B;i!=C;i=Fa[i]) c[len--]=i;
        if(dfs(1),num&1)
        {
            if(Abs(g[1][1]-g[1][0])&1) return 0*puts("-1");
            int dif=(g[1][1]-g[1][0])/2;ans=Abs(dif);
            for(int i=A;i;i=Fa[i]) g[i][0]+=dif;
            for(int i=B;i;i=Fa[i]) g[i][0]+=dif;
            for(int i=1;i<=n;++i) ans+=Abs(g[i][0]-g[i][1]);
            printf("%d\n",ans);
        }
        else
        {
            if(g[1][0]!=g[1][1]) return 0*puts("-1");
            for(int i=A;i!=C;i=Fa[i]) q[++top]=g[i][0]-g[i][1];
            for(int i=B;i!=C;i=Fa[i]) q[++top]=g[i][1]-g[i][0];
            sort(q+1,q+top+1);
            int res;
            if(top&1) res=q[(top+1)/2];
            else res=Abs(q[top/2])<Abs(q[top/2+1])?q[top/2]:q[top/2+1];
            res=-res;
            for(int i=A;i;i=Fa[i]) g[i][0]+=res;
            for(int i=B;i;i=Fa[i]) g[i][1]+=res;
            ans=Abs(res);for(int i=1;i<=n;++i) ans+=Abs(g[i][0]-g[i][1]);
            printf("%d\n",ans);
        }
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:77:13: error: 'sort' was not declared in this scope; did you mean 'short'?
   77 |             sort(q+1,q+top+1);
      |             ^~~~
      |             short
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] = -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()) ;
	
		ans = x ;

		for (int i = 0 ; i < n - 1 ; i ++) ans += abs( 

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

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


Compiler output:
a.cc: In function 'int main()':
a.cc:113:23: error: 'x' was not declared in this scope
  113 |                 ans = x ;
      |                       ^
a.cc:117:17: error: expected primary-expression before 'int'
  117 |                 int x = vec[(n - 1) / 2] ;
      |                 ^~~
Language: C++

// Namori
// * frank_c1
// * 2017 / 12 / 18
 
#include <bits/stdc++.h>
using namespace std;

const int maxn = (int)(1e5) + 5;
vector<int> nxt[maxn];
int fa[maxn], dfn[maxn], siz[maxn], su[maxn];
int ci[maxn], cc[maxn], va[maxn];
int cnt, idx;

inline int iabs(int x) {
	return x >= 0 ? x : -x;
}

void dfs(int x, int f, int v) {
	fa[x] = f; dfn[x] = ++idx; 
	siz[x] = 1; su[x] = v; v ^= 1;
	for (int i = 0; i < (int)nxt[x].size(); ++i)
		if (nxt[x][i] != f) {
			if (!dfn[nxt[x][i]]) {
				dfs(nxt[x][i], x, v);
				su[x] += su[nxt[x][i]];
				siz[x] += siz[nxt[x][i]];
			} else if (dfn[nxt[x][i]] < dfn[x]) {
				int c = x, a = nxt[x][i]; 
				while (c != a) {
					ci[++cnt] = c; cc[c] = 1; c = fa[c];
				} ++cnt;
			}
		}
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		nxt[u].push_back(v); nxt[v].push_back(u);
	}
	dfs(1, 0, 0);
	if (!cnt || !(cnt & 1)) {
		if (2 * su[1] != siz[1]) {
			printf("-1\n"); return;
		}
	} else {
		if ((2 * su[1] - siz[1]) & 1) {
			printf("-1\n"); return;
		}
	}
	int dt = (2 * su[1] - siz[1]) / 2, ret = 0;
	for (int i = 1; i <= n; ++i) if (!cc[i]) ret += iabs(2 * su[i] - siz[i]);
	if (cnt) {
		for (int i = 1; i < cnt; ++i) va[i] = 2 * su[ci[i]] - siz[ci[i]];
		va[cnt] = 0; sort(va + 1, va + cnt + 1); ret += iabs(va[(cnt + 1) / 2]) + iabs(dt);
		for (int i = 1; i < cnt; ++i) ret += iabs(va[(cnt + 1) / 2] - dt - 2 * su[ci[i]] + siz[ci[i]]);
	}
	printf("%d\n", ret);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:47:41: error: return-statement with no value, in function returning 'int' [-fpermissive]
   47 |                         printf("-1\n"); return;
      |                                         ^~~~~~
a.cc:51:41: error: return-statement with no value, in function returning 'int' [-fpermissive]
   51 |                         printf("-1\n"); return;
      |                                         ^~~~~~
Language: C++

// Namori
// * frank_c1
// * 2017 / 12 / 18
 
#include <bits/stdc++.h>
using namespace std;

const int maxn = (int)(1e5) + 5;
vector<int> nxt[maxn];
int fa[maxn], dfn[maxn], siz[maxn];
int cnt;

void dfs(int x, int f) {
	fa[x] = f; siz[x] = 1; dfn[x] = ++idx;
	for (int i = 0; i < (int)nxt[x].size(); ++i)
		if (nxt[x][i] != f) {
			if (!dfn[nxt[x][i]]) {
				dfs(nxt[x][i], x);
				siz[x] += siz[nxt[x][i]];
			} else if (dfn[nxt[x][i]] < dfn[x]) {
				cnt = 1;
				int c = x, a = nxt[x][i];
				while (c != a) {
					++cnt; c = fa[c];
				}
			}
		}
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		nxt[u].push_back(v); nxt[v].push_back(u);
	}
	dfs(1, 0);
	if (!cnt) {
		if (n & 1) {
			printf("-1\n");
		} else {
			int ret = 0;
			for (int i = 1; i <= n; ++i) ret += (siz[i] & 1);
			printf("%d\n", ret);
		}
	} else if (!(cnt & 1)) {
		
	} else {
		
	}
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:14:43: error: 'idx' was not declared in this scope
   14 |         fa[x] = f; siz[x] = 1; dfn[x] = ++idx;
      |                                           ^~~
Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,m,w[N],head[N],next[N];
void add(int f,int t){
	static int cnt=0;
	w[++cnt]=t;
	next[cnt]=head[f];
	head[f]=cnt;
}
int c1[N],c2[N];ll dp[N];
void dfs(int x,int h,int fa){
	(h&1?c1[x]:c2[x])=1;
	for (int i=head[x];i;i=next[i])
	if (w[i]!=fa){
		int v=w[i];
		dfs(v,h+1,x);
		dp[x]+=dp[v]+abs(c1[v]-c2[v]);
		c1[x]+=c1[v];
		c2[x]+=c2[v];
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v);add(v,u);
	}
	dfs(1,0,0);
	printf("%lld\n",c1[1]==c2[1]?dp[1]:-1ll);
	return 0;
}

Compiler output:
a.cc: In function 'void add(int, int)':
a.cc:9:9: error: reference to 'next' is ambiguous
    9 |         next[cnt]=head[f];
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 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:5:22: note:                 'int next [200010]'
    5 | int n,m,w[N],head[N],next[N];
      |                      ^~~~
a.cc: In function 'void dfs(int, int, int)':
a.cc:15:32: error: reference to 'next' is ambiguous
   15 |         for (int i=head[x];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:5:22: note:                 'int next [200010]'
    5 | int n,m,w[N],head[N],next[N];
      |                      ^~~~