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<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int P[20][100005];
vector<int> Sons[100005];
int Val[100005];
bool Ball[100005];
int Priority[100005];
set<pair<int,int> > S;
void dfs(int nod)
{
Val[nod]=nod;
for(auto son:Sons[nod])
{
dfs(son);
Val[nod]=min(Val[nod],Val[son]);
}
}
bool cmp(int a, int b)
{
return Val[a]<Val[b];
}
int nr;
void getPriority(int nod)
{
sort(Sons[nod].begin(),Sons[nod].end(),cmp);
for(auto son:Sons[nod])
getPriority(son);
Priority[nod]=++nr;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
int root=0;
for(int i=1; i<=n; i++)
{
cin>>P[0][i];
Sons[P[0][i]].push_back(i);
if(P[0][i]==0)
root=i;
}
for(int i=1; i<=16; i++)
{
for(int j=1; j<=n; j++)
P[i][j]=P[i-1][P[i-1][j]];
}
dfs(root);
getPriority(root);
for(int i=1; i<=n; i++)
S.insert({Priority[i],i});
for(int i=1; i<=q; i++)
{
int tip,x;
cin>>tip>>x;
if(tip==1)
{
int rez=0;
for(int j=1; j<=x; j++) //suma x-ilor va fi max n+q
{
int nod=(*S.begin()).second;
S.erase(S.begin());
Ball[nod]=1;
rez=nod;
}
cout<<rez<<"\n";
}
else
{
int rez=0;
for(int i=16; i>=0; i--)
{
if(Ball[P[i][x]])
{
rez+=(1<<i);
x=P[i][x];
}
}
cout<<rez<<"\n";
Ball[x]=0;
S.insert({Priority[x],x});
}
}
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... |