# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30525 | RezwanArefin01 | Ball Machine (BOI13_ballmachine) | C++14 | 1000 ms | 17660 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
vector<int> adj[maxn], post;
int prior[maxn];
int MinDfs(int u) {
if(!adj[u].size()) return u;
for(int v : adj[u])
prior[v] = MinDfs(v);
sort(adj[u].begin(), adj[u].end(), [](int &a, int &b) { return prior[a] < prior[b]; } );
}
void postDfs(int u) {
if(!adj[u].size()) { post.push_back(u); return; }
for(int v : adj[u]) postDfs(v);
post.push_back(u);
}
struct node{
int u, prior;
bool operator < (const node &p) const {
return prior < p.prior;
}
};
int main(int argc, char const *argv[]) {
int n, q, root;
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; i++) {
int p; scanf("%d", &p);
if(p == 0) root = i;
adj[p].push_back(i);
}
MinDfs(root);
postDfs(root);
set<node> Q;
for(int i = 0; i < n; i++) {
Q.insert({post[i], i});
prior[post[i]] = i;
}
while(q--) {
int c, x;
scanf("%d %d", &c, &x);
if(c == 1) {
node u;
while(x--) {
u = *(Q.begin());
Q.erase(Q.begin());
} printf("%d\n", u.u);
} else {
auto it = Q.find({x, prior[x]});
if(it != Q.end())
Q.erase(it);
puts("0");
}
}
}
Compilation message (stderr)
# | 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... |