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 <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n,m;
vector<int> adjlist[N];
int U[N], V[N];
int p[N], d[N], col[N], num[N];
int ans = 0;
void dfs2(int u){
    for (auto v : adjlist[u]){
        if (d[v] == d[u]+1){
            dfs2(v);
            num[u] += num[v];
        }
    }
    ans += abs(num[u]);
}
void dfs1(int u){
    for (auto v : adjlist[u]){
        if (col[v] == -1){
            d[v] = d[u]+1;
            col[v] = 1^col[u];
            p[v] = u;
            dfs1(v);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    if (n % 2 != 0){ printf("-1\n"); return 0;}
    for (int i = 0; i < m; i++){
        scanf("%d%d",&U[i],&V[i]);
        adjlist[U[i]].push_back(V[i]);
        adjlist[V[i]].push_back(U[i]);
    }
    memset(col,-1,sizeof(col));
    col[1] = 0;
    p[1] = 0;
    dfs1(1);
    for (int i = 1; i <= n; i++){
        num[i] = col[i]?-1:1;
    }
    dfs2(1);
    for (int i = 0; i < m; i++){
        if (p[U[i]] != V[i] && p[V[i]] != U[i]){
            if (d[U[i]] < d[V[i]]) swap(U[i],V[i]);
            //printf("%d %d\n",U[i],V[i]);
            if (col[U[i]] != col[V[i]]){
                //printf("even cycle");
                int cur = U[i];
                vector<int> li;
                while (cur != V[i]){
                    li.push_back(num[cur]);
                    cur = p[cur];
                }
                sort(li.begin(),li.end());
                int x = li[li.size()/2];
                cur = U[i];
                while (cur != V[i]){
                    num[cur] -= x;
                    cur = p[cur];
                }
                ans = abs(x);
                for (int i = 1; i <= n; i++){
                    ans += abs(num[i]);
                }
            }
            else{
                //printf("odd cycle\n");
                int x = num[V[i]]/2;
                //printf("difference is %d\n",num[V[i]]);
                for (int i = 1; i <= n; i++){
                    num[i] = col[i]?-1:1;
                }
                num[U[i]] -= x;
                num[V[i]] -= x;
                ans = abs(x);
                dfs2(1);
            }
            break;
        }
    }
    if (num[1]){
        printf("-1");
    }
    else printf("%d",ans);
    return 0;
}