#include <bits/stdc++.h>
#define INF 999999999
using namespace std;
vector<vector<int> > treeAdj;
vector<int> smallestInSubtree, freePlacesInSubtree, depth;
int n, q;
int root;
void computeData(int node, int d){
int chld = treeAdj[node].size();
depth[node] = d;
//printf("Getting data for node %d (depth %d)\n", node, d);
if(chld == 0){
//printf("IT S A LEAF!\n");
smallestInSubtree[node] = node;
freePlacesInSubtree[node] = 1;
return;
}
int sml = node;
int fps = 1;
int v;
vector<pair<int, int> > newSortedAdj;
for(int i = 0; i < chld; i++){
v = treeAdj[node][i];
//printf("%d -th child of %d is %d\n", i+1, node, v);
computeData(v, d+1);
newSortedAdj.push_back({smallestInSubtree[v], v});
sml = min(sml, smallestInSubtree[v]);
fps+= freePlacesInSubtree[v];
}
sort(newSortedAdj.begin(), newSortedAdj.end());
for(int i = 0; i < chld; i++){
treeAdj[node][i] = newSortedAdj[i].second;
}
smallestInSubtree[node] = sml;
freePlacesInSubtree[node] = fps;
//printf("Got data for node %d (depth %d)\n", node, d);
return;
}
void fillAllSubtree(int node){
if(freePlacesInSubtree[node] == 0) return;
freePlacesInSubtree[node] = 0;
for(int i = 0; i < treeAdj[node].size(); i++){
fillAllSubtree(treeAdj[node][i]);
}
}
int addBalls(int node, int val){ // returns new val after filling subtree of node
if(val == 0 || freePlacesInSubtree[node] <= 0) return val;
int freePlaces = freePlacesInSubtree[node];
if(freePlaces < val){
fillAllSubtree(node);
return val-freePlaces;
}
else if(freePlaces == val){
// This node is the one to be output
fillAllSubtree(node);
cout << node+1 << endl;
return 0;
}
int sz = treeAdj[node].size();
for(int i = 0; i < sz && val > 0; i++){
val = addBalls(treeAdj[node][i], val);
}
}
int removeBall(int node, int val, int *lastRolledDepth){
if(node == val){
return 1;
}
int sz = treeAdj[node].size();
if(sz == 0){
return 0;
}
bool nodeIsFree = freePlacesInSubtree[node] != 0;
//cout << "Is node " << node << " free " << (nodeIsFree?"1":"0") << endl;
int returnCode, v;
for(int i = 0; i < sz; i++){
v = treeAdj[node][i];
returnCode = removeBall(v, val, lastRolledDepth);
if(returnCode == 1){
if(!nodeIsFree){
if(node == root){
freePlacesInSubtree[root]++;
*lastRolledDepth = 0;
return 2;
}
else return 1;
}
freePlacesInSubtree[v]++;
*lastRolledDepth = depth[v];
return 2;
}
if(returnCode == 2) return 2;
}
return 0;
}
int main()
{
int p;
cin >> n >> q;
depth.resize(n);
treeAdj.resize(n);
smallestInSubtree.resize(n);
freePlacesInSubtree.resize(n);
for(int i = 0; i < n; i++){
cin >> p;
if(p == 0) root = i;
else{
treeAdj[p-1].push_back(i);
}
}
//cout << "Going to compute data" << endl;
computeData(root, 0);
//cout << "Data was computedd successfully" << endl;
int action, val;
int *lastRolledDepth;
for(int i = 0; i < q; i++){
cin >> action >> val;
if(action == 1){
addBalls(root, val);
}
else{
val--;
removeBall(root, val, lastRolledDepth);
//cout << "Last rolled depth is " << *lastRolledDepth << endl;
cout << depth[val] - *lastRolledDepth << endl;
}
}
return 0;
}
Compilation message
ballmachine.cpp: In function 'void fillAllSubtree(int)':
ballmachine.cpp:54:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < treeAdj[node].size(); i++){
~~^~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int addBalls(int, int)':
ballmachine.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:139:10: warning: 'lastRolledDepth' may be used uninitialized in this function [-Wmaybe-uninitialized]
int *lastRolledDepth;
^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
484 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
65 ms |
7160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
62 ms |
7160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
2 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
3 ms |
612 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
3 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
2 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
3 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
6 ms |
896 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
15 ms |
2224 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
58 ms |
7160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
64 ms |
7284 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
50 ms |
7160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
31 ms |
7864 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
164 ms |
26488 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
75 ms |
10728 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
32 ms |
8312 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
46 ms |
8184 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
41 ms |
8312 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
37 ms |
5624 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
30 ms |
8056 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
161 ms |
26232 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
189 ms |
26364 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
231 ms |
26532 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
172 ms |
18552 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
192 ms |
37124 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
100 ms |
10916 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
67 ms |
13388 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
159 ms |
18768 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
148 ms |
33664 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
123 ms |
21112 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
117 ms |
21240 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
129 ms |
21268 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
108 ms |
15224 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
135 ms |
33848 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
161 ms |
26460 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
151 ms |
26508 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
159 ms |
26360 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
137 ms |
18940 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
200 ms |
42104 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
89 ms |
10724 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
152 ms |
26404 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
167 ms |
19060 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
219 ms |
41988 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
153 ms |
26360 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
209 ms |
26520 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
182 ms |
26524 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
170 ms |
19016 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
231 ms |
41948 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
107 ms |
10868 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |