#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 4000005
#define ff first
#define ss second
#define pb push_back
#define sz(s) (int)s.size()
#define rep(c, a, b) for(c = a; c <= b; c++)
int n, t, q, m, vis[N], par[N], r;
vector <int> v[N];
deque <int> ans;
void solve(int x,int p) {
int mn = INT_MAX;
for(auto i : v[x]) {
if(!vis[i]) mn = min(mn,i);
}
if(mn == INT_MAX) {
vis[x] = 1;
if(!p){
cout << x << '\n';
}
return;
}
solve(mn, p);
return;
}
void find(int x,int cnt) {
if(vis[par[x]] == 0 || par[x] == x) {
vis[x] = 0;
cout << cnt << '\n';
return;
}
find(par[x], cnt + 1);
}
int main () {
ios::sync_with_stdio(0);cin.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++) {
int x;
cin >> x;
if(x) {
v[x].pb(i);
par[i] = x;
}
else r = i;
}
par[r] = r;
for(int i = 1; i <= q; i++) {
int x;
cin >> t >> x;
if(t == 1) {
while(x--) {
solve(r, x);
}
continue;
}
find(x,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... |