Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6379
@blackshadows 6329
@mb1973 5358
@Quandray 5231
@akhayrutdinov 5111
@saiujwal13083 4510
@sanjay05 3762
@marius_valentin_dragoi 3522
@sushant_a 3459
@verma_ji 3357
@KshamaGupta 3318
Complete Leaderboard
Geek and City
Easy Accuracy: 95.0% Submissions: 20 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.

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

Input:
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


Constraints:
1. 1 <= T <= 100

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


Example:
Input:

2
4 2
1 2
3 4

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

Output:
1
0

to report an issue on this page.

Editorial

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

Yes

All Submissions

My Submissions:

Login to access your submissions.

Geek and City

Output Window