# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
160485 | tushar_2658 | Ball Machine (BOI13_ballmachine) | C++14 | 1085 ms | 33784 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.
#include "bits/stdc++.h"
using namespace std;
const int maxn = 100005;
int par[maxn], root;
vector<int> edges[maxn];
int depth[maxn], dp[maxn], n, m;
void dfs(int x, int p){
depth[x] = (p != -1) ? depth[p] + 1 : 0;
dp[x] = x;
vector<pair<int, int>> all;
for(auto i : edges[x]){
if(i == p)continue;
dfs(i, x);
all.push_back({dp[i], i});
dp[x] = min(dp[x], dp[i]);
}
sort(all.begin(), all.end());
edges[x].clear();
for(auto i : all){
edges[x].push_back(i.second);
edges[i.second].push_back(x);
}
}
set<pair<int, int>> st;
int ed[maxn], cnt = 0;
void dfs1(int x, int p){
for(auto i : edges[x]){
if(i == p)continue;
dfs1(i, x);
}
ed[x] = ++cnt;
st.insert({cnt, x});
}
int sparse[maxn][22];
void build(){
for(int i = 1; i <= n; i++){
sparse[i][0] = par[i];
}
for(int j = 1; (1 << j) <= n; j++){
for(int i = 1; i <= n; i++){
if(sparse[i][j - 1] != 0){
sparse[i][j] = sparse[sparse[i][j - 1]][j - 1];
}
}
}
}
bool status[maxn];
int first_empty(int x){
for(int i = 20; i >= 0; i--){
if(sparse[x][i] != 0 && status[sparse[x][i]] != 0){
x = sparse[x][i];
}
}
return x;
}
int main(int argc, char const *argv[])
{
// freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++){
int x;
scanf("%d", &x);
if(x != 0){
edges[i].push_back(x);
edges[x].push_back(i);
par[i] = x;
}else {
root = i;
}
}
dfs(root, -1);
dfs1(root, -1);
build();
while(m--){
int t, x;
scanf("%d %d", &t, &x);
if(t == 1){
pair<int, int> last;
for(int i = 0; i < x; i++){
last = *st.begin();
st.erase(st.begin());
status[last.second] = 1;
}
printf("%d\n", last.second);
}else {
int ff = first_empty(x);
status[ff] = 0;
st.insert({ed[x], x});
printf("%d\n", depth[x] - depth[ff]);
}
}
return 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... |