-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS2.cpp
More file actions
72 lines (58 loc) · 1.2 KB
/
DFS2.cpp
File metadata and controls
72 lines (58 loc) · 1.2 KB
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
67
68
69
70
71
72
#include<bits/stdc++.h>
#define MAX 1000
#define WHITE 0
#define GREY 1
#define BLACK 2
using namespace std;
vector<int>G[MAX];
int curTime=0;
int dTime[MAX];
int fTime[MAX];
int visited[MAX];
void DFS(int u)
{
curTime+=1;
dTime[u]=curTime;
visited[u]=GREY;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(visited[v]==WHITE)
{
DFS(v);
}
}
curTime+=1;
fTime[u]=curTime;
visited[u]=BLACK;
}
int main()
{
freopen("DFS2.txt","r",stdin);
int nodes,edges,a,b;
int source;
//printf("Enter number of nodes:");
scanf("%d",&nodes);
//printf("Enter the number of edges:");
scanf("%d",&edges);
// printf("Enter edges:");
for(int i=1;i<=edges;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
//printf("Enter source:");
scanf("%d",&source);
for(int i=1;i<=nodes;i++)
{
visited[i]=WHITE;
}
DFS(source);
printf("Node\tD.Time\t\tF.Time\n");
for(int i=1;i<=nodes;i++)
{
printf("%d\t%d\t\t%d\n",i,dTime[i],fTime[i]);
}
return 0;
}