-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1057A.cpp
More file actions
65 lines (51 loc) · 1.15 KB
/
Copy path1057A.cpp
File metadata and controls
65 lines (51 loc) · 1.15 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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void dfs (vector<vector<ll>>& g, vector<bool>& vis, ll v, vector<ll>& parent);
void pathReconstruction(vector<ll>& parent, vector<ll>& path, ll n);
int main () {
ll n, inp; cin >> n;
vector<vector<ll>> g(n+10);
vector<bool> vis(n+10, 0);
vector <ll> parent(n+10, -1), path;
for (ll i = 2; i <= n; i++) {
cin >> inp;
if (inp == 1) {
g[1].push_back(i);
g[i].push_back(1);
} else {
g[i].push_back(inp);
g[inp].push_back(i);
}
}
dfs(g, vis, 1, parent);
pathReconstruction(parent, path, n);
for (auto it : path) {
cout << it << " ";
}
cout << endl;
}
void dfs (vector<vector<ll>>& g, vector<bool>& vis, ll v, vector<ll>& parent) {
stack <ll> s;
s.push(v);
vis[v] = 1;
while (!s.empty()) {
ll curr = s.top();
s.pop();
for (auto it : g[curr]) {
if (!vis[it]) {
vis[it] = 1;
s.push(it);
parent[it] = curr;
}
}
}
}
void pathReconstruction(vector<ll>& parent, vector<ll>& path, ll n) {
ll current = n;
while (current != -1) {
path.push_back(current);
current = parent[current];
}
reverse(path.begin(), path.end());
}