-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconnected_components.cpp
More file actions
executable file
·50 lines (50 loc) · 1.04 KB
/
connected_components.cpp
File metadata and controls
executable file
·50 lines (50 loc) · 1.04 KB
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
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define INF LLONG_MAX
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,m,i,j,x,a,b,u,v;
cin>>n>>m;
vector<ll> adj[n+1];
for(i=0;i<m;i++)
{
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
ll visited[n+1];
for(i=0;i<=n;i++)
{
visited[i]=0;
}
ll c=0;
for(j=1;j<=n;j++)
{
if(!visited[j])
{
visited[j]=1;
queue<ll> q;
q.push(j);
while(!q.empty())
{
x=q.front();
for(i=0;i<adj[x].size();i++)
{
if(!visited[adj[x][i]])
{
visited[adj[x][i]]=1;
q.push(adj[x][i]);
}
}
q.pop();
}
c++;
}
}
cout<<c<<endl;
return 0;
}