-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfsingraph.cpp
More file actions
42 lines (40 loc) · 943 Bytes
/
Copy pathdfsingraph.cpp
File metadata and controls
42 lines (40 loc) · 943 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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> g[N];
bool visited[N];
void dfs(int vertex)
{
if (visited[vertex])
return;
// cout << vertex << endl;
// Takes action on vertex after entering the vertex
visited[vertex] = 1;
for (int child : g[vertex])
{
cout<<vertex<<" "<<child<<endl;
// Takes action on child before entering the child node
if(child!=vertex)
dfs(child);
// Takes action on child after entering the child node
}
// Takes action on vertex before exiting the vertex
}
int main()
{
int n, m;
n = 5;
m = 7;
// cin >> n >> m;
for (int i = 0; i < m; i++)
{
int v1, v2;
cin >> v1 >> v2;
g[v1].push_back(v2);
g[v2].push_back(v1);
}
dfs(0);
// visited array size is no of nodes
// visited array represents the number of nodes visited
return 0;
}