-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
61 lines (53 loc) · 886 Bytes
/
dfs.cpp
File metadata and controls
61 lines (53 loc) · 886 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
58
59
60
61
#include<stdio.h>
#include<vector>
using namespace std;
#define SZ 100
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
int nV, nE, Time;
vector<int> edge[SZ];
int color[SZ], d[SZ], f[SZ], pi[SZ];
void dfs_visit(int u){
color[u] = GRAY;
Time = Time + 1;
d[u] = Time;
for(int i=0; i<edge[u].size(); i++){
int v = edge[u][i];
if( color[v] == WHITE ){
pi[v] = u;
dfs_visit(v);
}
}
color[u] = BLACK;
f[u] = Time = Time + 1;
}
void dfs(){
int u;
for(u=1; u<=nV; u++){
color[u] = WHITE;
pi[u] = -1;
}
Time = 0;
for(u=1; u<=nV; u++){
if( color[u] == WHITE ){
dfs_visit(u);
}
}
}
void input(){
int i, u, v;
printf("Insert number of node and number of edges :: ");
scanf("%d%d", &nV, &nE);
puts("Insert edges :: ");
for(i=0; i<nE; i++){
scanf("%d%d", &u, &v);
edge[u].push_back(v);
}
}
int main(){
input();
dfs();
return 0;
}