#include #include using namespace std; int main() { int n, q, tmp; cin >> n >> q; vector indexes(n, 0); vector > successor(n); vector cycle; for (int i = 1; i < n; ++i) { cin >> tmp; successor[tmp].push_back(i); } bool pattern = false; for (int i = 0; i < q; ++i) { // pattern known if (pattern) { cout << cycle[i % cycle.size()] << endl; continue; } // no pattern known int gate = 0; while (!successor[gate].empty()) { // not at list tmp = successor[gate][indexes[gate]]; indexes[gate] = (indexes[gate]+1) % (int)successor[gate].size(); gate = tmp; } cout << gate << endl; // attempt at finding pattern cycle.emplace_back(gate); if (i % 2 == 1) { // check cycle pattern = true; for (int j = 0; j <= (i / 2); ++j) { if (cycle[j] != cycle[(i/2 + 1) + j]) { pattern = false; break; } } } } return 0; }