-
Notifications
You must be signed in to change notification settings - Fork 2
/
11.dfs.cpp
66 lines (51 loc) · 973 Bytes
/
11.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
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
#define white 0
#define green 1
#define black 2
int tim = 0, d[100], f[100], color[100];
vector < int > adj[100];
int n, e;
void dfs(int n, int p);
void check(int n)
{
for (int i = 1; i <= n; i++)
{
if(color[i] == 0)
dfs(n,i);
}
}
void dfs(int n, int p)
{
int s = p;
tim++;
d[s] = tim;
color[s] = green;
cout << "d:"<<s<<" "<<d[s] << endl;
for(int i = 0; i < adj[s].size(); i++)
{
int p = adj[s][i];
if (color[p] == white)
{
dfs(n,p);
}
}
color[s] = black;
tim++;
f[s] = tim;
cout << "f:"<<s<<" "<< f[s] << endl;
}
int main()
{
cout << "Enter number of nodes and edges" << endl;
cin >> n >> e;
cout << "Enter pair of nodes" << endl;
for (int i = 0; i < e; i++)
{
int a, b;
cin >> a >> b;
adj[a].push_back(b);
}
check(n);
return 0;
}