-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cpp
More file actions
51 lines (42 loc) · 870 Bytes
/
dfs.cpp
File metadata and controls
51 lines (42 loc) · 870 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
#include<bits/stdc++.h>
using namespace std;
vector < vector <int> > adj;
int V,E,dfstime;
int discover[110];
int finished[110];
bool marked[110];
int parent[110];
void dfsvisit(int s)
{
discover[s] = ++dfstime;
marked[s] = 1;
for(auto v:adj[s])
if(!marked[v])
dfsvisit(v);
finished[s] = ++dfstime;
}
void dfs()
{
for(int i = 1; i < V+1; ++i)
parent[i] = -1,marked[i] = 0;
dfstime = 0;
for(int i = 1; i < V+1; ++i){
if(!marked[i])
dfsvisit(i);
}
}
int main()
{
cin>>V>>E;
adj.resize(V+10);
for(int i = 0; i < E; ++i){
int x,y; cin>>x>>y;
adj[x].push_back(y);
}
dfs();
for(int i = 0; i < V+1; ++i)
cout<<discover[i]<<(i==V?'\n':' ');
for(int i = 0; i < V+1; ++i)
cout<<finished[i]<<(i==V?'\n':' ');
return 0;
}