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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    int N;
    List<int>[] G;
    int A,B;
    bool[] even;
    bool[] b;
    int ce,co;
    long ans;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        N = int.Parse(str[0]);
        int M = int.Parse(str[1]);
        G = new List<int>[N];
        for(int i=0;i<N;i++){
            G[i] = new List<int>();
        }
        if(M == N-1){
            for(int i=0;i<M;i++){
                str = Console.ReadLine().Split(' ');
                int x = int.Parse(str[0])-1;
                int y = int.Parse(str[1])-1;
                G[x].Add(y);
                G[y].Add(x);
            }
        }
        else{
            UnionFind U = new UnionFind(N);
            for(int i=0;i<M;i++){
                str = Console.ReadLine().Split(' ');
                int x = int.Parse(str[0])-1;
                int y = int.Parse(str[1])-1;
                if(!U.Same(x,y)){
                    G[x].Add(y);
                    G[y].Add(x);
                    U.Union(x,y);
                }
                else{
                    A = x;
                    B = y;
                }
            }
        }
        even = new bool[N];
        b = new bool[N];
        ce = 0;
        co = 0;
        checkodd(0,true);
        if(M == N-1){
            if(ce != co){
                sb.Append("-1\n");
            }
            else{
                ans = 0;
                b = new bool[N];
                dfs(0);
                sb.Append(ans+"\n");
            }
        }
        else{
            if(even[A] != even[B]){
                if(ce != co){
                    sb.Append("-1\n");
                }
                else{
                    long bf = -N;
                    long bl = N;
                    while(bl - bf >= 3){
                        long bc1 = (2*bf+bl)/3 - ((2*bf+bl)%3 < 0 ? 1 : 0);
                        long bc2 = (bf+2*bl)/3 - ((bf+2*bl)%3 < 0 ? 1 : 0);
                        long a1 = check(bc1);
                        long a2 = check(bc2); 
                        if(a1 >= a2){
                            bf = bc1;
                        }
                        if(a1 <= a2){
                            bl = bc2;
                        }
                    }
                    for(long i=bf;i<=bl;i++){
                        bf = check(bf) < check(i) ? bf : i;
                    }
                    sb.Append(check(bf)+"\n");
                }
            }
            else{
                if(N % 2 == 1){
                    sb.Append("-1\n");
                }
                else{
                    ans = 0;
                    b = new bool[N];
                    dfs2(A,(ce-co)/2);
                    sb.Append(ans+"\n");
                }
            }
        }
    }
    long check(long ex){
        b = new bool[N];
        ans = 0;
        dfs2(A,ex);
        return ans;
    }
    long dfs2(int v,long ex){
        b[v] = true;
        long count = 0;
        if(v == B){
            count -= ex;
        }
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!b[t]){
                count += dfs2(t,ex);
            }
        }
        if(even[v]){
            count++;
        }
        else{
            count--;
        }
        ans += Math.Max(count,-count);
        return count;
    }
    long dfs(int v){
        b[v] = true;
        long count = 0;
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!b[t]){
                count += dfs(t);
            }
        }
        if(even[v]){
            count++;
        }
        else{
            count--;
        }
        ans += Math.Max(count,-count);
        return count;
    }
    void checkodd(int v,bool e){
        even[v] = e;
        b[v] = true;
        if(e){
            ce++;
        }
        else{
            co++;
        }
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(!b[t]){
                checkodd(t,!e);
            }
        }
    }
}
class UnionFind{
    int[] par;
    public UnionFind(int n){
        par = new int[n];
        for(int i=0;i<n;i++){
            par[i] = i;
        }
    }
    public void Union(int x,int y){
        par[Get(x)] = Get(y); 
    }
    public bool Same(int x,int y){
        return Get(x) == Get(y);
    }
    int Get(int x){
        if(x != par[x]){
            par[x] = Get(par[x]);
        }
        return par[x];
    }
}