-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreachableroads.cpp
49 lines (43 loc) · 1.06 KB
/
reachableroads.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> al;
vector<int> visited;
void dfs(int node)
{
//mark current node as visited
visited[node] = 1;
for(auto &it : al[node]) //check all neighbour in this node
if(!visited[it])
dfs(it);
}
int main()
{
freopen("test.txt", "r", stdin);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
while(n--)
{
int roadEndpoints, pair, output = 0;
cin >> roadEndpoints >> pair;
al.assign(roadEndpoints, vector<int>());
for(int i = 0; i < pair; i++)
{
int a, b;
cin >> a >> b;
al[a].push_back(b);
al[b].push_back(a);
}
visited.assign(roadEndpoints, 0);
// dfs(0);
for(int j = 0; j < visited.size(); j++)
if(!visited[j])
{
// cout << "not visited: " << j << endl;
output++;
dfs(j);
}
cout << output - 1 << endl;
}
return 0;
}