#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+7;
const int LOG = 19;
int jp[MAXN][LOG];
int ojciec[MAXN];
int depth[MAXN];
bool ball[MAXN];
vector<int> g[MAXN];
set<pair<int,int> > s;
int post=0;
void DFS(int x){
depth[x]=depth[ojciec[x]]+1;
jp[x][0]=ojciec[x];
for(int i=1;i<LOG;i++) jp[x][i]=jp[jp[x][i-1]][i-1];
for(int i=0;i<g[x].size();i++){
DFS(g[x][i]);
}
s.insert({++post,x});
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,q,x,r,a,b;
pair<int,int> p;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>x;
ojciec[i]=x;
if(x==0) r=i;
else g[x].push_back(i);
}
DFS(r);
while(q--){
cin>>a>>b;
if(a==1){
while(b--){
auto it=s.begin();
s.erase(it);
p=*it;
ball[p.second]=1;
if(b==0){
cout<<p.second<<'\n';
}
}
}
else{
x=b;
for(int i=LOG-1;i>=0;i--){
if(ball[jp[x][i]]==1){
x=jp[x][i];
}
}
ball[x]=0;
//cout<<b<<' '<<x<<'\n';
cout<<depth[b]-depth[x]<<'\n';
}
}
return 0;
}
Compilation message
ballmachine.cpp: In function 'void DFS(int)':
ballmachine.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i=0;i<g[x].size();i++){
| ~^~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:32:5: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
32 | DFS(r);
| ~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1040 ms |
4700 KB |
Time limit exceeded |
2 |
Execution timed out |
1058 ms |
13916 KB |
Time limit exceeded |
3 |
Incorrect |
45 ms |
14164 KB |
Output isn't correct |
4 |
Execution timed out |
1048 ms |
4700 KB |
Time limit exceeded |
5 |
Execution timed out |
1056 ms |
4700 KB |
Time limit exceeded |
6 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
7 |
Execution timed out |
1097 ms |
4700 KB |
Time limit exceeded |
8 |
Execution timed out |
1018 ms |
4696 KB |
Time limit exceeded |
9 |
Execution timed out |
1033 ms |
4952 KB |
Time limit exceeded |
10 |
Execution timed out |
1033 ms |
8024 KB |
Time limit exceeded |
11 |
Execution timed out |
1052 ms |
13648 KB |
Time limit exceeded |
12 |
Incorrect |
46 ms |
14044 KB |
Output isn't correct |
13 |
Execution timed out |
1060 ms |
13848 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1016 ms |
9816 KB |
Time limit exceeded |
2 |
Execution timed out |
1010 ms |
22228 KB |
Time limit exceeded |
3 |
Incorrect |
61 ms |
18384 KB |
Output isn't correct |
4 |
Execution timed out |
1033 ms |
10328 KB |
Time limit exceeded |
5 |
Execution timed out |
1016 ms |
9880 KB |
Time limit exceeded |
6 |
Execution timed out |
1066 ms |
10332 KB |
Time limit exceeded |
7 |
Execution timed out |
1050 ms |
9504 KB |
Time limit exceeded |
8 |
Execution timed out |
1061 ms |
9820 KB |
Time limit exceeded |
9 |
Execution timed out |
1065 ms |
22392 KB |
Time limit exceeded |
10 |
Execution timed out |
1054 ms |
22340 KB |
Time limit exceeded |
11 |
Execution timed out |
1030 ms |
22412 KB |
Time limit exceeded |
12 |
Execution timed out |
1051 ms |
20368 KB |
Time limit exceeded |
13 |
Incorrect |
65 ms |
24780 KB |
Output isn't correct |
14 |
Incorrect |
60 ms |
18384 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
14588 KB |
Output isn't correct |
2 |
Incorrect |
84 ms |
20836 KB |
Output isn't correct |
3 |
Correct |
67 ms |
23192 KB |
Output is correct |
4 |
Incorrect |
62 ms |
20052 KB |
Output isn't correct |
5 |
Incorrect |
69 ms |
19648 KB |
Output isn't correct |
6 |
Incorrect |
82 ms |
19592 KB |
Output isn't correct |
7 |
Incorrect |
55 ms |
18564 KB |
Output isn't correct |
8 |
Correct |
65 ms |
23288 KB |
Output is correct |
9 |
Incorrect |
85 ms |
22864 KB |
Output isn't correct |
10 |
Incorrect |
103 ms |
22356 KB |
Output isn't correct |
11 |
Incorrect |
85 ms |
22440 KB |
Output isn't correct |
12 |
Incorrect |
81 ms |
20852 KB |
Output isn't correct |
13 |
Correct |
99 ms |
26968 KB |
Output is correct |
14 |
Incorrect |
84 ms |
18644 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
22848 KB |
Time limit exceeded |
2 |
Execution timed out |
1048 ms |
20816 KB |
Time limit exceeded |
3 |
Incorrect |
76 ms |
26700 KB |
Output isn't correct |
4 |
Execution timed out |
1047 ms |
22828 KB |
Time limit exceeded |
5 |
Execution timed out |
1025 ms |
22100 KB |
Time limit exceeded |
6 |
Execution timed out |
1020 ms |
22452 KB |
Time limit exceeded |
7 |
Execution timed out |
1064 ms |
20820 KB |
Time limit exceeded |
8 |
Incorrect |
72 ms |
26708 KB |
Output isn't correct |
9 |
Incorrect |
61 ms |
18476 KB |
Output isn't correct |