This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<int> par, way, pos;
vector<bool> ball;
void dfs(int u) {
for (int v: g[u]) {
dfs(v);
}
way.emplace_back(u);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n, q;
cin >> n >> q;
g.assign(n + 1, {});
par.assign(n + 1, 0);
ball.assign(n + 1, false);
pos.assign(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> par[i];
g[par[i]].emplace_back(i);
}
way.clear();
dfs(0);
// dfs way created
set<pair<int, int>> emp;
for (int i = 0; i < n; i++) {
emp.insert({i, way[i]});
pos[way[i]] = i;
}
while (q--) {
int t, x;
cin >> t >> x;
if (t == 1) {
int i = 0, last = 0;
vector<pair<int, int>> del;
for (auto it = emp.begin(); i < x; i++, it++) {
last = (*it).second;
ball[(*it).second] = true;
del.emplace_back(*it);
}
for (auto it: del) {
emp.erase(it);
}
cout << last << '\n';
} else {
if (!ball[par[x]]) {
cout << "0\n";
continue;
}
int cnt = 0;
while (ball[par[x]]) {
x = par[x];
cnt++;
}
ball[x] = false;
emp.insert({pos[x], x});
cout << cnt << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |