Language: C++

#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> PP;
 
/*
    freopen("input","r",stdin);
    freopen("output","w",stdout);
*/
 
int N, M;
 
vector<unordered_set<int> > G;
vector<vector<int> > T;
 
ll res;
 
void build(int a, int b) {
    unordered_set<int>::iterator it;
    for (it = G[a].begin();it != G[a].end();++it) {
        int c = *it;
        if (c == b) continue;
        T[a].push_back(c);
        build(c, a);
    }
    return;
}
 
int cal(int a) {
    int now = 1; // Need one A.
    for (int i = 0;i < T[a].size();i++) {
        now -= cal(T[a][i]);
    }
    if (a != 1) {
        res += abs(now);
    }
    return now;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin >> N >> M;
    G.resize(N + 1);
    int a, b;
    for (int i = 0;i < M;i++) {
        cin >> a >> b;
        G[a].insert(b);
        G[b].insert(a);
    }
    if (N == M) {
        res = 0;
        queue<int> X;
        for (int i = 1;i <= N;i++) {
            if (G[i].size() == 1) X.push(i);
        }
        int need[100010];
        for (int i = 1;i <= N;i++) need[i] = 1;
        while (X.size() > 0) {
            a = X.front();
            X.pop();
            unordered_set<int>::iterator it;
            it = G[a].begin();
            b = *it;
            res += abs(need[a]);
            need[b] -= need[a];
            G[b].erase(a);
            if (G[b].size() == 1) X.push(b);
        }
        int start = -1;
        for (int i = 1;i <= N;i++) {
            if (G[i].size() == 2) {
                start = i;
                break;
            }
        }
        vector<ll> route;
        route.push_back(start);
        unordered_set<int>::iterator it;
        it = G[start].begin();
        int pre = start;
        int crt = *it;
        while (crt != start) {
            route.push_back(crt);
            it = G[crt].begin();
            if (*it == pre) it++;
            pre = crt;
            crt = *it;
        }
        // Let it go!
        vector<ll> cost, pos, neg, all;
        int zero = 0;
        for (int i = 0;i < route.size() - 1;i++) {
            res += abs(need[route[i]]);
            cost.push_back(need[route[i]]);
            need[route[i + 1]] -= need[route[i]];
        }
        // No solution.
        if (need[route[route.size() - 1]] % 2 != 0) {
            cout << -1 << endl;
            return 0;
        }
        // Make inverse flow.
 
        ll rem = need[route[route.size() - 1]] / 2;
        for (int i = 0;i < cost.size();i++) {
            res -= abs(cost[i]) - abs(cost[i] - rem);
            cost[i] -= rem;
            rem *= -1;
        }
        res += abs(rem);
        cost.push_back(-rem);
        if (route.size() % 2) {
            cout << res << endl;
            return 0;
        }
        for (int i = 0;i < cost.size();i++) {
            if (i % 2 == 0) {
                if (cost[i] > 0) pos.push_back(cost[i]);
                else if (cost[i] < 0) neg.push_back(cost[i]);
                else zero++;
            }
            else {
                if (cost[i] > 0) neg.push_back(-cost[i]);
                else if (cost[i] < 0) pos.push_back(-cost[i]);
                else zero++;
            }
            all.push_back(abs(cost[i]));
        }
        sort(pos.begin(), pos.end());
        sort(neg.begin(), neg.end());
        for (int i = 0; i * 2 < neg.size();i++) {
            swap(neg[i], neg[neg.size() - 1 - i]);
        }
        sort(all.begin(), all.end());
        ll num = all[all.size() / 2];
        for (int i = 0;i < pos.size();i++) {
            res -= abs[pos[i]] - abs(pos[i] - num);
        }
        for (int i = 0;i < neg.size();i++) {
            res -= abs[neg[i]] - abs(neg[i] + num);
        }
        res += zero * num;
        cout << res << endl;
        return 0;
    }
    else {
        T.resize(N + 1);
        build(1, 0);
        res = 0;
        int ok = cal(1);
        if (ok != 0) cout << -1 << endl;
        else cout << res << endl;
    }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:152:23: error: invalid types '<unresolved overloaded function type>[__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type {aka long long int}]' for array subscript
  152 |             res -= abs[pos[i]] - abs(pos[i] - num);
      |                       ^
a.cc:155:23: error: invalid types '<unresolved overloaded function type>[__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type {aka long long int}]' for array subscript
  155 |             res -= abs[neg[i]] - abs(neg[i] + num);
      |                       ^