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>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
using namespace std;
const int N=1e5+5;
int n,q,mn[N],pos[N],cnt,col[N],p[N][20];
vector<int>adj[N];
void dfs(int u,int par=-1)
{
for(int i=1;i<=18;i++) p[u][i]=p[p[u][i-1]][i-1];
mn[u]=u;
for(auto&v:adj[u])
{
if(v==par) continue;
p[v][0]=u;
dfs(v,u);
mn[u]=min(mn[u],mn[v]);
}
}
void go(int u,int par=-1)
{
vector<pii>order;
for(auto&v:adj[u]) if(v!=par) order.push_back(mp(mn[v],v));
sort(order.begin(),order.end());
for(auto&v:order) go(v.se,u);
pos[u]=++cnt;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>q;
int root;
for(int i=1;i<=n;i++)
{
int p;
cin>>p;
if(!p) root=i;
else adj[p].push_back(i);
}
dfs(root);
go(root);
set<pii>empty_node;
for(int i=1;i<=n;i++) empty_node.insert(mp(pos[i],i));
while(q--)
{
int type,x;
cin>>type>>x;
int res=0;
if(type==1)
{
while(x--)
{
auto it=empty_node.begin();
res=(*it).se;
col[res]=1;
empty_node.erase(it);
}
cout<<res<<'\n';
continue;
}
for(int i=18;i>=0;i--)
{
if(col[p[x][i]]==1)
{
res+=(1<<i);
x=p[x][i];
}
}
empty_node.insert(mp(pos[x],x));
col[x]=0;
cout<<res<<'\n';
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:48:7: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
48 | go(root);
| ~~^~~~~~
# | 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... |