-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdfs.cpp
42 lines (38 loc) · 796 Bytes
/
dfs.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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node {
bool visited;
vector<ll> adj;
};
void explore(vector<node> &graph, ll u) {
graph[u].visited = true;
for(ll i=0; i<graph[u].adj.size(); i++) {
ll v = graph[u].adj[i];
if(!graph[v].visited) {
explore(graph,v);
}
}
}
void dfs(vector<node> &graph, ll n) {
for(ll i=1; i<=n; i++) {
if(!graph[i].visited) {
explore(graph,i);
}
}
}
int main() {
ll n,e,i,s,u,v,d;
cin>>n>>e;
vector<node> graph(n+1);
for(i=0;i<n;i++) {
graph[i].visited = false;
}
for(i=0;i<e;i++) {
cin>>u>>v;
graph[u].adj.push_back(v);
graph[v].adj.push_back(u);
}
dfs(graph,n);
return 0;
}