# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
336511 | Lecii | Ball Machine (BOI13_ballmachine) | C++14 | 1094 ms | 33772 KiB |
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;
typedef pair<int,int> pii;
int n,m;
int u,v;
vector<int> g[100005];
vector<pii> g2[100005];
int root;
int mi[100005];
bool a[100005];
int d[100005];
int pa[100005][25];
int L;
int las;
void dfs(int u,int dis)
{
d[u]=dis;
mi[u]=u;
for(int i=0;i<g[u].size();i++)
{
pa[g[u][i]][0]=u;
dfs(g[u][i],dis+1);
mi[u]=min(mi[u],mi[g[u][i]]);
g2[u].push_back({mi[g[u][i]],g[u][i]});
}
if(!g2[u].empty())
sort(g2[u].begin(),g2[u].end());
}
int dfs2(int u,int b)
{
if(b<=0) return 0;
for(int i=0;i<g2[u].size();i++)
{
if(!a[g2[u][i].second]) b=dfs2(g2[u][i].second,b);
if(b<=0) return 0;
}
a[u]=1;
las=u;
--b;
return b;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m;
L=__lg(n)+5;
for(int i=1;i<=n;i++)
{
cin>>u;
if(u==0) root=i;
else
g[u].push_back(i);
}
pa[root][0]=root;
dfs(root,0);
for(int j=1;j<=L;j++)
for(int i=1;i<=n;i++)
pa[i][j]=pa[pa[i][j-1]][j-1];
while(m--)
{
cin>>u>>v;
if(u==1)
{
dfs2(root,v);
cout<<las<<endl;
}
else
{
int u=v;
for(int i=L;i>=0;i--)
if(a[pa[v][i]])
v=pa[v][i];
a[v]=0;
cout<<d[u]-d[v]<<endl;
}
}
return 0;
}
Compilation message (stderr)
# | 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... |