이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int32_t main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N, Q;
cin >> N >> Q;
vector<int> g[N];
int root = -1;
for (int i = 0;i < N;i ++) {
int p;
cin >> p;
-- p;
if (p != -1) g[p].push_back(i);
else root = i;
}
assert(root != -1);
const int lg = 20;
vector<vector<int>> up(lg, vector<int>(N, 0));
vector<int> tin(N, 0), tout(N, 0), submn(N), depth(N, 0);
int _time = 0;
function<void(int, int)> dfs_init = [&](int si, int pi) -> void {
up[0][si] = pi;
for (int i = 1;i < lg;i ++) {
up[i][si] = up[i - 1][up[i - 1][si]];
}
submn[si] = si;
tin[si] = ++ _time;
for (auto v : g[si]) if (v != pi) {
depth[v] = depth[si] + 1;
dfs_init (v, si);
submn[si] = min(submn[si], submn[v]);
}
tout[si] = _time;
};
dfs_init (root, root);
vector<int> all;
auto cmp = [&](int x, int y) -> bool {
return submn[x] < submn[y];
};
function<void(int, int)> dfs = [&](int si, int pi) -> void {
sort (g[si].begin(), g[si].end(), cmp);
// cout << si << " ";
// for (auto v : g[si]) if (v != pi) {
// cout << v << " ";
// } cout << "\n";
for (auto v : g[si]) if (v != pi) {
dfs (v, si);
}
all.push_back(si);
};
dfs (root, root);
// for (auto v : all) cout << v << " ";
// cout << "\n";
vector<int> val(N + 1);
set<array<int, 2>> index;
for (int i = 0;i < all.size();i ++) {
val[all[i]] = i;
index.insert({i, all[i]});
}
auto is_ancestor = [&](int x, int y) -> bool {
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
};
vector<int> used(N, 0);
while (Q --) {
int typ, x;
cin >> typ >> x;
if (typ == 1) {
for (int i = 0;i < x;i ++) {
assert (index.size() > 0);
if (i == x - 1) {
cout << depth[(*index.begin())[1]] + 1 << "\n";
}
used[(*index.begin())[1]] = 1;
index.erase(index.begin());
}
} else {
-- x;
int lc = x;
for (int bt = lg - 1;bt >= 0;bt --) if (used[up[bt][lc]] == 1) {
lc = up[bt][lc];
}
used[lc] = 0;
index.insert({val[lc], lc});
cout << depth[x] - depth[lc] << "\n";
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int32_t main()':
ballmachine.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0;i < all.size();i ++) {
| ~~^~~~~~~~~~~~
ballmachine.cpp:71:10: warning: variable 'is_ancestor' set but not used [-Wunused-but-set-variable]
71 | auto is_ancestor = [&](int x, int y) -> bool {
| ^~~~~~~~~~~
# | 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... |