시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 1024 MB66342853.846%

문제

You are given an undirected graph $G=(V,E)$.

If you are drawing in the edges one by one, how many edges did you add when the graph becomes connected for the first time.

If the graph never becomes connected after drawing all the edges, output -1.

Here are some useful definitions:

  • A graph is connected if between any two vertices $u,v \in V$, you can reach $u$ from $v$ along a path of edges.
  • A graph is undirected if every edge from a vertex $u$ to a vertex $v$ allows travel from vertex $v$ to vertex $u$ as well.

입력

The input line of input contains two space-separated integers $N$ $(1 \leq N \leq 10^6)$ and $M$ $(0 \leq M \leq \min(10^6, \binom{N}{2}))$, the number of vertices and edges in the graph.

Each of the remaining $M$ lines contains two space-separated integers $u_i$ and $v_i$ $(1 \leq u_i, v_i \leq N)$, denoting an undirected edge from node $u_i$ to node $v_i$. No edge connects a node to itself, and there is at most one edge between any pair of nodes.

출력

Print either the number of edges that you added the first time graph becomes connected and -1 otherwise.

예제 입력 1

2 0

예제 출력 1

-1

예제 입력 2

1 0

예제 출력 2

0

예제 입력 3

7 11
1 4
1 5
1 6
1 7
2 3
3 4
3 5
3 6
4 7
5 6
6 7

예제 출력 3

6

출처

ICPC > Regionals > North America > Mid-Central Regional > 2024 Mid-Central USA Programming Contest E번

  • 문제를 만든 사람: Christian Yongwhan Lim