이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<int> p, order, depth, pos;
vector< vector<int> > children, up;
vector<int> used;
int root, cnt, max_log;
pair<int, vector<int> > dfs(int u, int par, int d) {
int mn = u;
depth[u] = d;
vector< pair<int, vector<int> > > tmp;
up[u][0] = par;
for (int i = 1; i <= max_log; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (auto v : children[u]) {
tmp.push_back(dfs(v, u, d + 1));
mn = min(mn, tmp.back().first);
}
sort(tmp.begin(), tmp.end());
vector<int> ans;
for (auto& v : tmp) {
for (auto& x : v.second) {
ans.push_back(x);
}
}
ans.push_back(u);
return {mn, ans};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
max_log = (int) ceil(log2((double) n));
up.assign(n + 1, vector<int>(max_log + 1));
p.resize(n + 1);
children.resize(n + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
if (p[i] == 0) {
root = i;
} else {
children[p[i]].push_back(i);
}
}
depth.resize(n + 1);
pos.resize(n + 1);
order = dfs(root, root, 0).second;
for (int i = 0; i < n; i++) {
pos[order[i]] = i;
}
used.assign(n + 1, 0);
set<int> st;
for (int i = 1; i <= n; i++) {
st.insert(pos[i]);
}
while (q--) {
int type, k;
cin >> type >> k;
if (type == 1) {
int last;
set<int> newSt;
for (auto& x : st) {
if (k == 0) {
newSt.insert(x);
} else {
k--;
used[order[x]] = 1;
last = order[x];
}
}
st = newSt;
cout << last << '\n';
} else {
int oldK = k;
for (int i = max_log; i >= 0; i--) {
if (used[up[k][i]]) {
k = up[k][i];
}
}
used[k] = 0;
st.insert(k);
cout << depth[oldK] - depth[k] << '\n';
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:76:23: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << last << '\n';
^~~~
# | 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... |