#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];
int mini[MAXN];
int post[MAXN];
bool ball[MAXN];
vector<int> g[MAXN];
set<pair<int,int> > s;
int cnt=0;
void DFS(int x){
//cout<<"wchodze x="<<x<<'\n';
depth[x]=depth[ojciec[x]]+1;
vector<pair<int,int> > vec;
for(int i=0;i<g[x].size();i++){
vec.push_back({mini[g[x][i]], g[x][i]});
//cout<<g[x][i]<<' '<<mini[g[x][i]]<<'\n';
}
sort(vec.begin(),vec.end());
for(int i=0;i<vec.size();i++) DFS(vec[i].second);
s.insert({++cnt,x});
post[x]=cnt;
}
void DFS1(int x){
for(int i=0;i<g[x].size();i++){
DFS1(g[x][i]);
}
mini[x]=x;
for(int i=0;i<g[x].size();i++) mini[x]=min(mini[x],g[x][i]);
}
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);
}
DFS1(r);
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;
while(x>0){
if(ball[ojciec[x]]) x=ojciec[x];
else break;
}
ball[x]=0;
s.insert({mini[x],x});
cout<<depth[b]-depth[x]<<'\n';
}
}
return 0;
}
Compilation message
ballmachine.cpp: In function 'void DFS(int)':
ballmachine.cpp:18:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i=0;i<g[x].size();i++){
| ~^~~~~~~~~~~~
ballmachine.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0;i<vec.size();i++) DFS(vec[i].second);
| ~^~~~~~~~~~~
ballmachine.cpp: In function 'void DFS1(int)':
ballmachine.cpp:28:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i=0;i<g[x].size();i++){
| ~^~~~~~~~~~~~
ballmachine.cpp:32:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0;i<g[x].size();i++) mini[x]=min(mini[x],g[x][i]);
| ~^~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:46:5: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
46 | DFS(r);
| ~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
2 |
Execution timed out |
1092 ms |
8892 KB |
Time limit exceeded |
3 |
Incorrect |
38 ms |
9080 KB |
Output isn't correct |
4 |
Execution timed out |
1062 ms |
4700 KB |
Time limit exceeded |
5 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
7 |
Execution timed out |
1094 ms |
4700 KB |
Time limit exceeded |
8 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
9 |
Incorrect |
4 ms |
4952 KB |
Output isn't correct |
10 |
Execution timed out |
1069 ms |
5724 KB |
Time limit exceeded |
11 |
Incorrect |
63 ms |
8828 KB |
Output isn't correct |
12 |
Incorrect |
38 ms |
9044 KB |
Output isn't correct |
13 |
Incorrect |
54 ms |
8792 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
8284 KB |
Output isn't correct |
2 |
Incorrect |
172 ms |
17492 KB |
Output isn't correct |
3 |
Incorrect |
47 ms |
10964 KB |
Output isn't correct |
4 |
Execution timed out |
1032 ms |
8536 KB |
Time limit exceeded |
5 |
Incorrect |
51 ms |
8532 KB |
Output isn't correct |
6 |
Incorrect |
43 ms |
8540 KB |
Output isn't correct |
7 |
Execution timed out |
1084 ms |
7528 KB |
Time limit exceeded |
8 |
Incorrect |
24 ms |
8284 KB |
Output isn't correct |
9 |
Incorrect |
76 ms |
17360 KB |
Output isn't correct |
10 |
Incorrect |
162 ms |
17220 KB |
Output isn't correct |
11 |
Incorrect |
72 ms |
17252 KB |
Output isn't correct |
12 |
Incorrect |
76 ms |
14224 KB |
Output isn't correct |
13 |
Incorrect |
122 ms |
21584 KB |
Output isn't correct |
14 |
Incorrect |
47 ms |
10964 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
762 ms |
11284 KB |
Output is correct |
2 |
Execution timed out |
1055 ms |
14400 KB |
Time limit exceeded |
3 |
Execution timed out |
1102 ms |
19792 KB |
Time limit exceeded |
4 |
Execution timed out |
1033 ms |
14852 KB |
Time limit exceeded |
5 |
Execution timed out |
1057 ms |
14752 KB |
Time limit exceeded |
6 |
Incorrect |
938 ms |
14932 KB |
Output isn't correct |
7 |
Incorrect |
983 ms |
12564 KB |
Output isn't correct |
8 |
Execution timed out |
1044 ms |
20328 KB |
Time limit exceeded |
9 |
Execution timed out |
1062 ms |
17492 KB |
Time limit exceeded |
10 |
Execution timed out |
1067 ms |
16980 KB |
Time limit exceeded |
11 |
Execution timed out |
1046 ms |
17232 KB |
Time limit exceeded |
12 |
Execution timed out |
1014 ms |
14592 KB |
Time limit exceeded |
13 |
Execution timed out |
1091 ms |
23632 KB |
Time limit exceeded |
14 |
Correct |
72 ms |
10960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1058 ms |
17532 KB |
Time limit exceeded |
2 |
Incorrect |
906 ms |
14488 KB |
Output isn't correct |
3 |
Execution timed out |
1027 ms |
23908 KB |
Time limit exceeded |
4 |
Execution timed out |
1036 ms |
17584 KB |
Time limit exceeded |
5 |
Execution timed out |
1062 ms |
17320 KB |
Time limit exceeded |
6 |
Incorrect |
72 ms |
17236 KB |
Output isn't correct |
7 |
Execution timed out |
1043 ms |
14340 KB |
Time limit exceeded |
8 |
Execution timed out |
1073 ms |
24124 KB |
Time limit exceeded |
9 |
Incorrect |
48 ms |
10964 KB |
Output isn't correct |