-
Notifications
You must be signed in to change notification settings - Fork 8
/
connected-components.cpp
55 lines (53 loc) · 1.23 KB
/
connected-components.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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mkp make_pair
typedef long double ld;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll m;
cin>>m;
ll x,y;
vector<ll> adj[n];
for(ll i=0;i<m;i++)
{
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<bool> visited(n,false);
ll c=0;
for(ll i=0;i<n;i++)
{
if(visited[i]==false)
{
c++;
stack<ll> s;
s.push(i);
visited[i]=true;
while(!s.empty())
{
ll p=s.top();
s.pop();
for(ll j=0;j<adj[p].size();j++)
{
if(!visited[adj[p][j]])
{
s.push(adj[p][j]);
visited[adj[p][j]]=true;
}
}
}
}
}
cout<<c<<endl;
}
return 0;
}