-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAM5.cpp
59 lines (55 loc) · 1.08 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
#include <bits/stdc++.h>
using namespace std;
#define lld long long int
vector<int> g[100005];
int vis[100005];
void dfs(int a)
{
stack <int> s;
s.push(a);
vis[a]=1;
while(!s.empty())
{
int val=s.top();
s.pop();
for(int i=0;i<g[val].size();i++)
{
if(vis[g[val][i]]==0)
{
s.push(g[val][i]);
vis[g[val][i]]=1;
}
}
}
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lld t;
cin>>t;
while(t--)
{
for(int i=0;i<100005;i++){g[i].clear();}
memset(vis,0,sizeof(vis));
int n,k,a,b;
cin>>n;
cin>>k;
while(k--)
{
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
int res=0;
for(int i=0;i<n;i++)
{
if(!vis[i])
{
res++;
dfs(i);
}
}
cout<<res<<"\n";
}
}