-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.cpp
61 lines (55 loc) · 1.01 KB
/
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
//
// main.cpp
// dfs
//
// Created by Zosia on 28.08.2017.
// Copyright © 2017 Zosia . All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
const int N=1000*1000+6;
vector<int> as[N];
bool vis[N];
int pre[N];
int post[N];
int x, y; // vertices, edges
int from, to;
int counter_pre=1, counter_post=1; // only if graph is a tree
void dfs(int curr)
{
vis[curr]=true;
pre[curr]=counter_pre;
counter_pre++;
for (auto &i:as[curr])
{
if (vis[i]==false)
{
dfs(i);
}
}
post[curr]=counter_post;
counter_post++;
}
int main()
{
ios_base::sync_with_stdio(0);
cin>>x>>y;
for (int i=0; i<y; i++)
{
cin>>from>>to;
as[from].push_back(to);
as[to].push_back(from);
}
dfs(1);
for (int i=1; i<=x; i++)
{
cout<<pre[i]<<" "; // pre-order
}
cout<<"\n";
for (int i=1; i<=x; i++)
{
cout<<post[i]<<" "; // post-order
}
cout<<"\n";
}