This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
8 4
0
1
2
2
3
3
4
6
1 8
2 5
2 7
2 8
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define f first
#define s second
inline pii mp(int a, int b)
{
pii temp;temp.f=a;temp.s=b;return temp;
}
int n, q;vector<int> adjlist[100001];
int root;
int parent[100001][20], mval[100001];
void dfs(int i)
{
mval[i]=i;
for(auto j:adjlist[i])
{
dfs(j);mval[i]=min(mval[i], mval[j]);
}
}
int cur, val[100001];bool done[100001];
void dfs2(int i)
{
for(auto j:adjlist[i])
{
dfs2(j);
}
val[i]=cur++;
}
struct comp
{
bool operator() (const int &a, const int &b)
{
return val[b]<val[a];
}
};
priority_queue<int, vector<int>, comp > pq;
bool sf(int a, int b)
{
return mval[a]<mval[b];
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
int f;cin>>f;
if(f==0) {root=i;}
else {adjlist[f].push_back(i);}
parent[i][0]=f;
}
for(int j=1;j<20;j++)
{
for(int i=1;i<=n;i++)
{
parent[i][j]=parent[parent[i][j-1]][j-1];
}
}
dfs(root);
for(int i=1;i<=n;i++)
{
sort(adjlist[i].begin(), adjlist[i].end(), sf);
}
cur=1;dfs2(root);
for(int i=1;i<=n;i++)
{
pq.push(i);
}
while(q--)
{
int t, k;cin>>t>>k;
if(t==1)
{
int ans;
while(k--)
{
ans=pq.top();done[ans]=1;pq.pop();
}
cout<<ans<<endl;
}
else
{
int ans=0;
int j=19;
while(j>=0)
{
if(done[parent[k][j]]) {k=parent[k][j];ans+=(1<<j);}
j--;
}
// ans++;
done[k]=0;cout<<ans<<endl;pq.push(k);
}
}
}
# | 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... |