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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int findMinSubtree(int node, vector<int> children[], int minSubtree[])
{
if (minSubtree[node] > 0)
return minSubtree[node];
int temp = node;
for (int child : children[node])
temp = min(temp, findMinSubtree(child, children, minSubtree));
minSubtree[node] = temp;
return temp;
}
void visitOrdered(int node, vector<int> children[], vector<int> &order)
{
for (int child : children[node])
visitOrdered(child, children, order);
order.push_back(node);
}
int main()
{
int n, q;
cin >> n >> q;
int parent[n + 5];
int minSubtree[n + 5];
vector<int> children[n + 5];
int root;
for (int i = 1; i <= n; i++)
{
minSubtree[i] = 0;
cin >> parent[i];
if (parent[i] == 0)
root = i;
else
children[parent[i]].push_back(i);
}
findMinSubtree(root, children, minSubtree);
for (int i = 1; i <= n; i++)
{
sort(children[i].begin(), children[i].end(), [&minSubtree](const int node1, const int node2)
{ return minSubtree[node1] < minSubtree[node2]; });
}
vector<int> orderToNode, nodeToOrder(n + 5);
visitOrdered(root, children, orderToNode);
for (int i = 0; i < orderToNode.size(); i++)
nodeToOrder[orderToNode[i]] = i;
priority_queue<int, vector<int>, greater<int>> orderToFill;
int r = 0;
for (int i = 0; i < q; i++)
{
int t, k;
cin >> t >> k;
if (t == 1)
{
int lastFilled = -1;
while (k > 0 && !orderToFill.empty())
{
lastFilled = orderToNode[orderToFill.top()];
orderToFill.pop();
k--;
}
if (k == 0)
{
cout << lastFilled << endl;
}
else
{
r += k;
cout << orderToNode[r-1] << endl;
}
}
else
{
orderToFill.push(nodeToOrder[k]);
cout << 0 << endl;
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < orderToNode.size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:55:17: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | visitOrdered(root, children, orderToNode);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |