Problem:
Score : 2200 points

Problem Statement

You are given an undirected graph with N vertices and M edges.
Here, N-1≤M≤N holds and the graph is connected.
There are no self-loops or multiple edges in this graph.

The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i connects vertices a_i and b_i.

The color of each vertex can be either white or black.
Initially, all the vertices are white.
Snuke is trying to turn all the vertices black by performing the following operation some number of times:

Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.

Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.

Constraints

2≤N≤10^5

N-1≤M≤N

1≤a_i,b_i≤N

There are no self-loops or multiple edges in the given graph.

The given graph is connected.

Partial Score

In the test set worth 1500 points, M=N-1.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective.
Otherwise, print -1 instead.

Sample Input 1

6 5
1 2
1 3
1 4
2 5
2 6

Sample Output 1

5

For example, it is possible to perform the operations as shown in the following diagram:

Sample Input 2

3 2
1 2
2 3

Sample Output 2

-1

It is not possible to turn all the vertices black.

Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

2

This case is not included in the test set for the partial score.

Sample Input 4

6 6
1 2
2 3
3 1
1 4
1 5
1 6

Sample Output 4

7

This case is not included in the test set for the partial score.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#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++;
            }
            if (cost[i] != 0) 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());
        if (zero >= all.size()) {
            cout << res << endl;
            return 0;
        }
        int idx = all.size() - zero - 1;
        ll num = all[idx];
        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;
}