#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++; } }; 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() { 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(); vector repeat; vector finalResults; for(int i = 0; i < q; i++) { repeat.push_back(drop(allGates, 0)); if(countWithZero == allGates.size()) { copy(repeat.begin(), repeat.end(), back_inserter(finalResults)); int rep = i + 1; while( i + rep < q) { i += rep; copy(repeat.begin(), repeat.end(), back_inserter(finalResults)); } repeat.clear(); } } copy(repeat.begin(), repeat.end(), back_inserter(finalResults)); for(int finalResult : finalResults) { cout << finalResult << '\n'; } return 0; }