-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.cpp
More file actions
53 lines (49 loc) · 781 Bytes
/
practice.cpp
File metadata and controls
53 lines (49 loc) · 781 Bytes
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
#include<bits/stdc++.h>
using namespace std;
map<int,bool>visited;
map<int,list<int>>adj;
void addEdge(int v,int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void DFSutil(int v)
{
visited[v] = true;
// cout<<v<<" ";
list<int>::iterator i;
for(i=adj[v].begin();i!=adj[v].end();i++)
{
if(visited[*i] == false)
{
DFSutil(*i);
}
}
}
void DFS(int n,int e)
{
int count = 0;
for(int i=1;i<=n;i++)
{
if(visited[i] == false)
{
DFSutil(i);
// cout<<endl;
count++;
}
}
cout<<count;
}
int main()
{
int n,e;
cin>>n>>e;
for(int i=0;i<e;i++)
{
int u,v;
cin>>u>>v;
addEdge(u,v);
// cout<<u<<" "<<v<<endl;
}
DFS(n,e);
}