-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS_Adj_List.cpp
More file actions
57 lines (47 loc) · 760 Bytes
/
DFS_Adj_List.cpp
File metadata and controls
57 lines (47 loc) · 760 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
#define lli long long int
#define mod 1000000007
using namespace std;
bitset<100001> visited;
vector< vector< int >> adj;
void dfs(int v)
{
visited[v] = 1;
cout<<v<<" ";
for ( int c : adj[v] )
if(!visited[c])
dfs(c);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cout<<"Enter number of vertices\n";
cin>>n;
int edge;
cout<<"Enter number of Edges\n";
cin>>edge;
int x,y;
adj.resize(n);
for ( int i = 0; i < edge; ++ i )
{
cin>>x>>y;
adj[x].push_back(y);
}
dfs(0); // dfs starting from vertex 0
return 0;
}
//0 1
//0 2
//1 2
//2 0
//2 3
//3 3
//0 -> 1 -> 2
//1 -> 2
//2 -> 0 -> 3
//3 -> 3
//output
//0 1 2 3