Language: C++

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100000 + 10;

struct Edge {
    int v, n;
} e[MAXN << 1];
int G[MAXN], edgeCnt;

void _add_edge(int u, int v)
{
    e[++edgeCnt].n = G[u];
    G[u] = edgeCnt;
    e[edgeCnt].v = v;
}

void add_edge(int u, int v)
{
    _add_edge(u, v);
    _add_edge(v, u);
}

int n, m;
int f[MAXN];
int g[MAXN];
int h[MAXN];
int size[MAXN];

bool vis[MAXN];
int foo, bar;

void dfs1(int u, int fa)
{
    size[u] = f[u] ? -1 : 1;
    size[u] += f[u] ? -h[u] : h[u];
    for (int i = G[u]; i; i = e[i].n)
        if (e[i].v != fa) {
            if (u == bar && e[i].v == foo
                || u == foo && e[i].v == bar) {
                    continue;
            }
            f[e[i].v] = f[u] ^ 1;
            dfs1(e[i].v, u);
            size[u] += size[e[i].v];
            g[u] += g[e[i].v];
        }
}

bool dfs2(int u, int fa)
{
    if (vis[u])
        return false;
    vis[u] = true;
    for (int i = G[u]; i; i = e[i].n)
        if (e[i].v != fa) {
            if (!dfs2(e[i].v, u))
                foo = u, bar = e[i].v;
        }
    return true;
}

int main()
{
    // freopen("sapphire.in", "r", stdin);
    // freopen("sapphire.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        add_edge(u, v);
    }
    if (m == n - 1) {
        dfs1(1, -1);
        long long ans = 0;
        for (int i = 1; i <= n; ++i)
            ans += abs(size[i]);
        cout << (size[1] == 0 ? ans : -1) << endl;
    } else {
        dfs2(1, 0);
        g[foo] = 1;
        g[bar] = -1;
        dfs1(1, 0);
        long long ans = 0;
        if (f[foo] == f[bar]) {
            if (abs(size[1]) & 1)
                cout << "-1\n";
            else {
                ans += abs(size[1]) / 2;
                h[foo] = h[bar] = ans;
                for (int i = 1; i <= n; ++i)
                dfs1(1, 0);
                    ans += abs(size[i]);
                cout << ans << endl;

            }
        } else {
            if (size[1] != 0)
                cout << "-1\n";
            else {
                static int a[MAXN], cnt = 0;
                for (int i = 1; i <= n; ++i) {
                    if (g[i] == 0) {
                        ans += abs(size[i]);
                    } else {
                        a[++cnt] = -g[i] * size[i];
                    }
                }
                nth_element(a + 1, a + (1 + cnt) / 2, a + cnt + 1);
                int temp = a[(1 + cnt) / 2];
                for (int i = 1; i <= n; ++i)
                    if (g[i] != 0) {
                        ans += abs(size[i] + g[i] * temp);
                    }
                ans += (f[foo] ^ 1 ? -1 : 1) * temp;
                cout << ans << endl;
            }
        }
    }
}

Compiler output:
a.cc: In function 'void dfs1(int, int)':
a.cc:36:5: error: reference to 'size' is ambiguous
   36 |     size[u] = f[u] ? -1 : 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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:37:5: error: reference to 'size' is ambiguous
   37 |     size[u] += f[u] ? -h[u] : h[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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:46:13: error: reference to 'size' is ambiguous
   46 |             size[u] += size[e[i].v];
      |             ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:46:24: error: reference to 'size' is ambiguous
   46 |             size[u] += size[e[i].v];
      |                        ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:79:24: error: reference to 'size' is ambiguous
   79 |             ans += abs(size[i]);
      |                        ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:80:18: error: reference to 'size' is ambiguous
   80 |         cout << (size[1] == 0 ? ans : -1) << endl;
      |                  ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:88:21: error: reference to 'size' is ambiguous
   88 |             if (abs(size[1]) & 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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:91:28: error: reference to 'size' is ambiguous
   91 |                 ans += abs(size[1]) / 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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:95:32: error: reference to 'size' is ambiguous
   95 |                     ans += abs(size[i]);
      |                                ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:95:37: error: 'i' was not declared in this scope
   95 |                     ans += abs(size[i]);
      |                                     ^
a.cc:100:17: error: reference to 'size' is ambiguous
  100 |             if (size[1] != 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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:106:36: error: reference to 'size' is ambiguous
  106 |                         ans += abs(size[i]);
      |                                    ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:108:44: error: reference to 'size' is ambiguous
  108 |                         a[++cnt] = -g[i] * size[i];
      |                                            ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~
a.cc:115:36: error: reference to 'size' is ambiguous
  115 |                         ans += abs(size[i] + g[i] * temp);
      |                                    ^~~~
/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:29:5: note:                 'int size [100010]'
   29 | int size[MAXN];
      |     ^~~~