-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFG_DFS.cpp
61 lines (56 loc) · 1.21 KB
/
GFG_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
//Position this line where user code will be pasted.
int main()
{
int T;
cin>>T;
while(T--)
{
int N, E;
cin>>N>>E;
vector<int> g[N];
bool vis[N];
memset(vis, false, sizeof(vis));
for(int i=0;i<E;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0,g,vis);
cout<<endl;
}
}
/*This is a function problem.You only need to complete the function given below*/
/* Function to do DFS of graph
* g[]: array of vectors to represent graph
* vis[]: array to keep track of visited vertex
*/
void dfs(int s, vector<int> g[], bool vis[])
{
stack<int>stk;
stk.push(s);
while(!stk.empty())
{
int i,z;
z=stk.top();
int fond=0;
if(vis[z]==false)
{
cout<<z<<" ";
vis[z]=true;
}
for(i=0;i<g[z].size();i++)
{
if(vis[g[z][i]]==false)
{
fond++;
stk.push(g[z][i]);
break;
}
}
if(fond==0)stk.pop();
}
}