-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dfs.cpp
62 lines (41 loc) · 887 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
//no of vertices is n
//no of edegs is e
int n,e;
cin>>n;
cin>>e;
//using adj list methood...!!
vector<int>g[n];
bool vis[n];
//set all the values... use memset
memset(vis,false,sizeof(vis));
for(int i=0;i<e;i++)
{
int u,v;
cin>>u;
cin>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0,g,vis);
cout<<endl;
}
}
void dfs (int s,vector<int>g[],bool *vis )
{
vis[s] = true;
cout<<" ";
for(int i=0;i<g[s].size();i++)
{
if(vis[g[s][i]] == false)
dfs(vis[g[s][i]],g,vis);
}
}
//all about dfs(depth for search)