forked from duanjigui/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDepth-First-Search.cpp
More file actions
65 lines (50 loc) · 767 Bytes
/
Depth-First-Search.cpp
File metadata and controls
65 lines (50 loc) · 767 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
62
63
64
65
#include <cstdio>
#define VERTEX_COUNT 100000
#define EDGE_COUNT 100000
using namespace std;
struct vertex
{
int first_edge;
int ftime;
}V[VERTEX_COUNT];
int latest = 1;
struct edge
{
int endp, next;
}E[EDGE_COUNT];
int ec = 1;
void add_edge(int u, int v)
{
E[ec].next = V[u].first_edge;
V[u].first_edge = ec;
E[ec].endp = v;
ec++;
}
void DFS(int u)
{
if (V[u].ftime == 0)
{
for (int cur = V[u].first_edge; cur != 0; cur = E[cur].next)
{
DFS(E[cur].endp);
}
V[u].ftime = latest++;
}
}
int main()
{
int ivc, iec;
scanf("%d%d", &ivc, &iec);
for (int i = 0; i < iec; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
}
DFS(1);
for (int i = 1; i <= ivc; i++)
{
printf("%d\n", V[i].ftime);
}
return 0;
}