-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
37 lines (34 loc) · 855 Bytes
/
Copy pathDFS.cpp
File metadata and controls
37 lines (34 loc) · 855 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
// define data structures
vector<vector<int>> adj; // adjacency list
int n; // number of nodes -> n
// DFS
vector<bool> visited;
void dfs(int v){
visited[v] = true;
for(int u: adj[v]){
if(!visited[u]){
dfs(u);
}
}
}
// DFS with entry and exit time;
vector<int> mark, time_in, time_out;
int counter=0;
void dfs_timer(int v){
mark[v] = 1; // mark visited
time_in[v] = counter++; // entry time of v
for(int u: adj[v]){
if(mark[u]==0){ // not visited
dfs_timer(u);
}
}
mark[v] = 2; // exited
time_out[v] = time_out++; // exit time of v
}
return 0;
}