아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Wooksin-ness of A Graph

면접 대비

시간 제한1초메모리 제한1024 MB

요약
단순 무방향 그래프가 주어질 때 사이클이 생기도록 추가해야 하는 최소 간선 수를 구하고, 간선을 더 넣을 수 없으면 -1을 출력한다.
난이도

보통10점 중 5점

유형
그래프, 유니온 파인드, 그리디, 구현
정답자
아직 제출이 없습니다

문제

At the headquarters of algospot.com, Toivoa “the chairman” has been pestering Astein “the slave” for a new graph problem for so long. So Astein came up with the following problem.

Wooksin-ness(욱신함) of an undirected graph G(V,E)G(V, E) is defined as the minimum number of additional edges in order to have a cycle in the graph. Stated formally, you can write it as:

min⁡∣E′−E∣\min |E' - E| s.t.s.t. E′⊇EE' ⊇ E and G′(V,E′)G'(V, E') has at least one cycle

However, loops (edges that start and end at the same vertex) and multiple edges between a single pair of vertices are not allowed.

Write a program that calculates Wooksin-ness of given graph.

입력

The input consists of TT test cases. The number of test cases TT is given in the first line of the input.

The first line of each test case contains two integers VV and EE (1≤V≤1001 ≤ V ≤ 100, 0≤E≤1,0000 ≤ E ≤ 1\\,000), where VV represents the number of vertices in the graph, and EE represents the number of edges. The vertices are numbered from 00 to V−1V - 1. The following EE lines will each contain two integers, which are the number of two vertices connected by an edge.

There will be no loops in the input data. There will be at most one edge between a pair of vertices.

출력

Print exactly one line for each test case. The line should contain an integer indicating the minimum number of additional edges we need to add to the graph to get a cycle. If this is impossible, print -1 instead.

예제1

  1. 예제 1

    입력
    2
    3 2
    0 1
    2 0
    3 3
    0 1
    1 2
    2 0
    
    예상 출력
    1
    0