이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define N 100000
vector<unsigned> g[N], anc[N];
unsigned min_child[N], postorder[N];
bitset<N> has_ball;
unsigned calc_min_child(unsigned u)
{
min_child[u] = u;
for (unsigned const &v : g[u])
min_child[u] = min(min_child[u], calc_min_child(v));
return min_child[u];
}
unsigned traverse(unsigned u, vector<unsigned> &path, unsigned i = 0)
{
for (size_t j = 1; j <= path.size(); j <<= 1)
anc[u].push_back(path[path.size() - j]);
path.push_back(u);
for (unsigned const &v : g[u])
i = traverse(v, path, i);
path.pop_back();
postorder[u] = i++;
return i;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, q;
cin >> n >> q;
unsigned r;
for (size_t i = 0; i < n; i++)
{
unsigned p;
cin >> p;
if (!p)
r = i;
else
g[p - 1].push_back(i);
}
calc_min_child(r);
for (size_t i = 0; i < n; i++)
sort(g[i].begin(), g[i].end(), [&](unsigned a, unsigned b)
{ return min_child[a] < min_child[b]; });
vector<unsigned> path;
traverse(r, path);
priority_queue<pair<unsigned, unsigned>> qu;
for (unsigned i = 0; i < n; i++)
qu.emplace(n - postorder[i], i);
while (q--)
{
unsigned t, x;
cin >> t >> x;
if (t == 1)
{
for (size_t i = 0; i < x; i++)
{
if (i + 1 == x)
cout << qu.top().second + 1 << '\n';
has_ball[qu.top().second] = 1;
qu.pop();
}
}
else
{
x--;
unsigned h = 0;
for (size_t i = anc[x].size() - 1; i < anc[x].size(); i--)
if (has_ball[anc[x][i]])
x = anc[x][i], h += (1U << i);
has_ball[x] = 0;
qu.emplace(n - postorder[x], x);
cout << h << '\n';
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:57:13: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
57 | traverse(r, path);
| ~~~~~~~~^~~~~~~~~
# | 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... |