#include using namespace std; typedef long long ll; typedef unsigned long long ull; int countWithZero = 0; struct gate { int id; vector children; int leftmostOpenGate = 0; void incOpened() { if (leftmostOpenGate == 0) countWithZero--; leftmostOpenGate++; leftmostOpenGate %= children.size(); if (leftmostOpenGate == 0) countWithZero++; } }; vector findIt(vector &gates, int node) { vector result; if (gates[node].children.size() == 0) { result.push_back(node); return result; } else { vector> deti; int sizeOfLargestChild = -1; for (int i = 0; i < gates[node].children.size(); i++) { auto vec = findIt(gates, gates[node].children[i]); if ((int)vec.size() > sizeOfLargestChild) sizeOfLargestChild = vec.size(); deti.push_back(vec); } for (int j = 0; j < sizeOfLargestChild; j++) { for (int i = 0; i < deti.size(); i++) { int idx = j % deti[i].size(); result.push_back(deti[i][idx]); } } } return result; } int drop(vector &gates, int id) { if (gates[id].children.size() == 0) { return id; } int nextId = gates[id].children[gates[id].leftmostOpenGate]; gates[id].incOpened(); return drop(gates, nextId); } int main(void) { ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; vector allGates; allGates.push_back({0, {}, 0}); for (int i = 1; i < n; i++) { allGates.push_back({i, {}, 0}); int pred; cin >> pred; allGates[pred].children.push_back(i); } countWithZero = allGates.size(); auto res = findIt(allGates, 0); for (int i = 0 ; i < q; i++) { int idx = i % res.size(); cout << res[idx] << endl; } return 0; }