-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam5.cpp
65 lines (58 loc) · 1.26 KB
/
cam5.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
#define MAX 100002
#define mp make_pair
#define pb push_back
using namespace std ;
vector<int> graph[MAX];
bool visited[MAX];
void initialize(int n){
for(int i = 0 ; i < n ; i++){
visited[i] = false ;
graph[i].clear();
}
}
void dfs(int source, vector<int> graph[]){
stack<int> s;
s.push(source);
while(!s.empty()){
int item = s.top() ;
s.pop();
visited[item] = true;
for(int i = 0 ;i <graph[item].size() ; i++){
int temp = graph[item][i];
if(!visited[temp])
s.push(graph[item][i]);
}
}
}
void pa(int n){
for(int i = 0 ; i < n ; i++)
cout << visited[i] << " " ;
cout << endl ;
}
int main()
{
int t, n ;
cin >> t ;
while(t--){
cin >> n ;
initialize(n);
int e, a, b ;
cin >> e ;
for(int i = 0 ;i < e; i++){
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
// Processing
int count = 0;
for(int i = 0 ;i < n ; i++){
if(!visited[i]){
dfs(i, graph) ;
count ++ ;
}
}
cout << count << endl ;
}
return 0;
}