C - 3 Steps Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500500

問題文

りんごさんは NN 頂点 の連結な無向グラフを持っています。 このグラフにはすでに MM 本の辺があり、ii 本目の辺は頂点 AiA_i と頂点 BiB_i を繋いでいます。

りんごさんは以下の操作を行うことで、辺を追加しようと思っています。

  • 操作:頂点 uu から辺をちょうど 33 本辿ることによって頂点 vv に辿り着けるような u,v(uv)u,v (u \neq v) をとり、頂点 uu と頂点 vv の間に辺を追加する。ただし、すでに頂点 uu と頂点 vv の間に辺が存在する場合は辺を追加することはできない。

りんごさんが追加できる辺の本数の最大値を求めて下さい。

制約

  • 2N1052 \leq N \leq 10^5
  • 1M1051 \leq M \leq 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 多重辺や自己ループは存在しない
  • 与えられるグラフは連結である

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1 B1B_1
A2A_2 B2B_2
::
AMA_M BMB_M

出力

追加できる辺の本数の最大値を出力せよ。


入力例 1Copy

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

出力例 1Copy

Copy
4

下の図のように辺を追加していくと 44 本の辺を追加することができ、これ以上辺を追加することはできません。


入力例 2Copy

Copy
5 5
1 2
2 3
3 1
5 4
5 1

出力例 2Copy

Copy
5

例えば、以下のような順番で辺を追加することによって 55 本の辺を追加することができます。

  • 頂点 55 と頂点 33 の間に辺を追加する。
  • 頂点 55 と頂点 22 の間に辺を追加する。
  • 頂点 44 と頂点 11 の間に辺を追加する。
  • 頂点 44 と頂点 22 の間に辺を追加する。
  • 頂点 44 と頂点 33 の間に辺を追加する。

Score : 500500 points

Problem Statement

Rng has a connected undirected graph with NN vertices. Currently, there are MM edges in the graph, and the ii-th edge connects Vertices AiA_i and BiB_i.

Rng will add new edges to the graph by repeating the following operation:

  • Operation: Choose uu and vv (uv)(u \neq v) such that Vertex vv can be reached by traversing exactly three edges from Vertex uu, and add an edge connecting Vertices uu and vv. It is not allowed to add an edge if there is already an edge connecting Vertices uu and vv.

Find the maximum possible number of edges that can be added.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1M1051 \leq M \leq 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • The graph has no self-loops or multiple edges.
  • The graph is connected.

Input

Input is given from Standard Input in the following format:

NN MM
A1A_1 B1B_1
A2A_2 B2B_2
::
AMA_M BMB_M

Output

Find the maximum possible number of edges that can be added.


Sample Input 1Copy

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

Sample Output 1Copy

Copy
4

If we add edges as shown below, four edges can be added, and no more.


Sample Input 2Copy

Copy
5 5
1 2
2 3
3 1
5 4
5 1

Sample Output 2Copy

Copy
5

Five edges can be added, for example, as follows:

  • Add an edge connecting Vertex 55 and Vertex 33.
  • Add an edge connecting Vertex 55 and Vertex 22.
  • Add an edge connecting Vertex 44 and Vertex 11.
  • Add an edge connecting Vertex 44 and Vertex 22.
  • Add an edge connecting Vertex 44 and Vertex 33.


2025-04-17 (Thu)
19:07:58 +00:00