-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs_for_graph.cpp
More file actions
35 lines (33 loc) · 884 Bytes
/
dfs_for_graph.cpp
File metadata and controls
35 lines (33 loc) · 884 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
//No need of parent node,instead of this we use visited array
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
vector<int>adj[N];
//vector<pair<int,int>>adj[N] for weighted graph.
bool visited[N];
void dfs(int node){
visited[node]=true;
//This part is for taking action after entering the vertex.
for(auto child : adj[node]){
//This part is for taking action on child before entering the child.
if(visited[child]){
continue;
}
dfs(child);
//Take action on child after exiting the child node.
}
//Take action on the node before exiting the node.
}
int main(){
int v,e;
cin>>v>>e;
for(int i=0;i<e;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
//adj[a].push_back({b,w}); for weighted graph
//adj[b].push_back({a,w});
}
return 0;
}