-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholliePS-335.cpp
72 lines (68 loc) · 1.36 KB
/
olliePS-335.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
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ull unsigned long long
#define flash() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const long long int MOD = 1e9 + 7;
const long long int INF = 1e18;
const long long int MAX = 1e9;
bool dfs(vector<int> adj[] , vector<ll>& vis , int src , int dest , vector<ll>& path)
{
if(src == dest)
{
return true;
}
vis[src] = 1;
for(auto node : adj[src])
{
if(vis[node] == 0)
{
bool found = dfs(adj , vis , node , dest , path);
if(found == true)
{
path.push_back(node);
return true;
}
}
}
return false;
}
void solution()
{
ll n;
cin >> n;
vector<ll> v(n+1 , 0);
for(ll i = 2 ; i <= n ; i++)
{
cin >> v[i];
}
vector<int> adj[n+1];
for(ll i = 2 ; i <= n ; i++)
{
adj[i].push_back(v[i]);
adj[v[i]].push_back(i);
}
vector<ll> visited(n + 1 , 0) , path;
bool found = dfs(adj , visited , 1 , n , path);
if(found == true)
{
path.push_back(1);
reverse(path.begin() , path.end());
}
for(auto it : path)
{
cout << it << " ";
}
cout << endl;
return;
}
int main()
{
flash();
ll t = 1;
while(t--)
{
solution();
}
return 0;
}