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;
int par[100005][17], mn[100005], t[100005], tt;
vector<int> child[100005];
set<pair<int, int> > s;
bool ball[100005];
bool cmp(int u, int v)
{
return mn[u]<mn[v];
}
void dfs(int u)
{
mn[u]=u;
for (int v:child[u])
{
dfs(v);
mn[u]=min(mn[u], mn[v]);
}
}
void dfs2(int u)
{
sort(child[u].begin(), child[u].end(), cmp);
for (int v:child[u])
dfs2(v);
t[u]=++tt;
// cout << "t[" << u << "]=" << t[u] << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
for (int i=1; i<=n; i++)
{
cin >> par[i][0];
child[par[i][0]].push_back(i);
}
for (int i=1; i<=16; i++)
for (int j=1; j<=n; j++)
par[j][i]=par[par[j][i-1]][i-1];
dfs(1);
dfs2(1);
for (int i=1; i<=n; i++)
s.insert({t[i], i});
for (int i=1; i<=q; i++)
{
int x, y;
cin >> x >> y;
if (x==1)
{
for (int j=1; j<=y; j++)
{
int u=s.begin()->second;
s.erase({t[u], u});
ball[u]=1;
// cout << "set ball[" << u << "] to 1\n";
if (j==y)
cout << u << '\n';
}
}
else
{
int cnt=0;
for (int j=16; j>=0; j--)
{
if (ball[par[y][j]])
{
y=par[y][j];
cnt+=1<<j;
}
}
s.insert({t[y], y});
ball[y]=0;
// cout << "set ball[" << y << "] to 0\n";
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... |