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