Connectedness
시간 제한1초메모리 제한1024 MB
주어진 무방향 간선을 하나씩 추가해 나가며 그래프가 처음 연결되는 순간까지 추가한 간선 수를 구한다.
문제
You are given an undirected graph .
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 , you can reach from along a path of edges.
- A graph is undirected if every edge from a vertex to a vertex allows travel from vertex to vertex as well.
입력
The input line of input contains two space-separated integers and , the number of vertices and edges in the graph.
Each of the remaining lines contains two space-separated integers and , denoting an undirected edge from node to node . 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.