-
Notifications
You must be signed in to change notification settings - Fork 47
/
dfs.c++
49 lines (41 loc) Β· 956 Bytes
/
dfs.c++
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
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000];
int num_v = 0;
void insertion_in_adj_list()
{
int n;
cin >> n;
while (n--)
{
int a, b;
cin >> a >> b;
adj[a].push_back(b);
}
}
void dfs_rec(int num_of_v,int start,vector<bool> &visited)
{
visited[start]=true;
cout<<start<<" ";
for(auto x: adj[start])
if(visited[x]==false)
dfs_rec(num_of_v,x,visited);
}
void dfs_ver1(int num_of_v, int start)
{
vector<bool> visited(num_of_v, false);
dfs_rec(num_of_v,start,visited);
//above is enough to get all the possible visitable vertices from start
for(int i=0;i<num_of_v;i++)
if(visited[i]==false)
dfs_rec(num_of_v,i,visited);
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
insertion_in_adj_list();
int num_of_v = 5;
int start = 0;
dfs_ver1(num_of_v, start);
}