#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<int>> al, ral;
vector<int> mn;
vector<int> ball;
int root;
void mndfs(int u) {
mn[u]=u;
for (int v: al[u]) {
mndfs(v);
mn[u]=min(mn[u],mn[v]);
}
}
int add_ball() {
int curr=root;
while (true) {
int n=-1;
int cmn=1e9;
for (int v: al[curr]) {
if (mn[v]<cmn&&!ball[v]) {
cmn=mn[v];
n=v;
}
}
if (n==-1) {
ball[curr]=1;
return curr;
}
curr=n;
}
}
int main() {
int n, q; cin>>n>>q;
al.assign(n+1,vector<int>());
ral.assign(n+1,vector<int>());
mn.assign(n+1,1e9);
ball.assign(n+1,0);
for (int i=1; i<=n; i++) {
int p; cin>>p;
if (p==0) {
root=i;
} else {
al[p].push_back(i);
ral[i].push_back(p);
}
}
mndfs(root);
while (q--) {
int t; cin>>t;
if (t==1) {
int k; cin>>k;
int ans;
for (int i=0; i<k; i++) {
ans=add_ball();
}
cout<<ans<<"\n";
} else {
int x; cin>>x;
ball[x]=0;
cout<<0<<"\n";
}
}
return 0;
}
Compilation message
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:61:24: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | cout<<ans<<"\n";
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
164 ms |
8420 KB |
Output isn't correct |
3 |
Correct |
157 ms |
8272 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
12 ms |
940 KB |
Output isn't correct |
10 |
Incorrect |
33 ms |
2368 KB |
Output isn't correct |
11 |
Incorrect |
165 ms |
8276 KB |
Output isn't correct |
12 |
Correct |
169 ms |
8320 KB |
Output is correct |
13 |
Incorrect |
161 ms |
8272 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1025 ms |
3672 KB |
Time limit exceeded |
2 |
Execution timed out |
1071 ms |
13908 KB |
Time limit exceeded |
3 |
Execution timed out |
1038 ms |
11472 KB |
Time limit exceeded |
4 |
Execution timed out |
1045 ms |
4700 KB |
Time limit exceeded |
5 |
Execution timed out |
1035 ms |
4440 KB |
Time limit exceeded |
6 |
Execution timed out |
1069 ms |
4444 KB |
Time limit exceeded |
7 |
Execution timed out |
1065 ms |
3932 KB |
Time limit exceeded |
8 |
Execution timed out |
1045 ms |
3676 KB |
Time limit exceeded |
9 |
Execution timed out |
1026 ms |
14424 KB |
Time limit exceeded |
10 |
Execution timed out |
1030 ms |
13912 KB |
Time limit exceeded |
11 |
Execution timed out |
1073 ms |
13904 KB |
Time limit exceeded |
12 |
Execution timed out |
1052 ms |
12636 KB |
Time limit exceeded |
13 |
Execution timed out |
1067 ms |
15708 KB |
Time limit exceeded |
14 |
Execution timed out |
1061 ms |
11472 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1040 ms |
7512 KB |
Time limit exceeded |
2 |
Execution timed out |
1094 ms |
12880 KB |
Time limit exceeded |
3 |
Execution timed out |
1037 ms |
13992 KB |
Time limit exceeded |
4 |
Execution timed out |
1042 ms |
11612 KB |
Time limit exceeded |
5 |
Execution timed out |
1051 ms |
11344 KB |
Time limit exceeded |
6 |
Execution timed out |
1100 ms |
11344 KB |
Time limit exceeded |
7 |
Execution timed out |
1061 ms |
10324 KB |
Time limit exceeded |
8 |
Execution timed out |
1075 ms |
14172 KB |
Time limit exceeded |
9 |
Execution timed out |
1048 ms |
14416 KB |
Time limit exceeded |
10 |
Execution timed out |
1038 ms |
14160 KB |
Time limit exceeded |
11 |
Execution timed out |
1056 ms |
13916 KB |
Time limit exceeded |
12 |
Execution timed out |
1060 ms |
12852 KB |
Time limit exceeded |
13 |
Execution timed out |
1049 ms |
17488 KB |
Time limit exceeded |
14 |
Execution timed out |
1051 ms |
10968 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1101 ms |
14424 KB |
Time limit exceeded |
2 |
Execution timed out |
1027 ms |
12880 KB |
Time limit exceeded |
3 |
Execution timed out |
1054 ms |
17492 KB |
Time limit exceeded |
4 |
Execution timed out |
1048 ms |
14420 KB |
Time limit exceeded |
5 |
Execution timed out |
1073 ms |
13916 KB |
Time limit exceeded |
6 |
Execution timed out |
1048 ms |
13992 KB |
Time limit exceeded |
7 |
Execution timed out |
1047 ms |
12880 KB |
Time limit exceeded |
8 |
Execution timed out |
1047 ms |
17500 KB |
Time limit exceeded |
9 |
Execution timed out |
1008 ms |
11468 KB |
Time limit exceeded |