AtCoder Grand Contest 004
F
Namori
AtCoder
⌀ 3986
原题
← E→
翻译中…

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.

翻译中…

样例

Input #1
6 5
1 2
1 3
1 4
2 5
2 6
Output #1
5
For example, it is possible to perform the operations as shown in the following diagram: ![image](https://atcoder.jp/img/agc/004/gatbantar/F_1.png)
Input #2
3 2
1 2
2 3
Output #2
-1
It is not possible to turn all the vertices black. ![image](https://atcoder.jp/img/agc/004/gatbantar/F_2.png)
Input #3
4 4
1 2
2 3
3 4
4 1
Output #3
2
This case is not included in the test set for the partial score. ![image](https://atcoder.jp/img/agc/004/gatbantar/F_3.png)
Input #4
6 6
1 2
2 3
3 1
1 4
1 5
1 6
Output #4
7
This case is not included in the test set for the partial score. ![image](https://atcoder.jp/img/agc/004/gatbantar/F_4.png)
题目信息
题号
agc004_f
时间限制
2 s
内存限制
256 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。