Geek and City
Easy Accuracy: 59.26% Submissions: 990 Points: 2

Given N cities, some of them have roads in between them. Geek is in city 1. The task is to find the minimum number of roads needed to construct so that geek can visit any city from any other city.

1. All the cities are numbered from 1 to N.
2. No two cities have multiple roads.
3. No city has self road

1. The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space_separated integers N and M.
3. Next M lines contain two space-separated integers u and v, represents a road between cities u and v

Output: For each test case, print the answer

1. 1 <= T <= 100

2. 1 <= N <= 50
3. 0 <= M <= min(1000, N*(N-1)/2)


4 2
1 2
3 4

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


We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Geek and City

Output Window