이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
using namespace std;
const int N = (int)1e5 + 2;
const int logN = 20;
typedef pair<int, int> pii;
int p[N][logN + 1], del[N], d[N], deg[N];
int Min[N], order[N], cur = 0;
int root, n, q, x, k, low, high, mid;
vector<int> a[N];
set<pii> leaf;
void DFS(int u) {
deg[u] = a[u].size(), Min[u] = u;
for(int i = 1; i <= logN; ++i) p[u][i] = p[p[u][i - 1]][i - 1];
for(int v: a[u]) {
d[v] = d[u] + 1, DFS(v);
Min[u] = min(Min[u], Min[v]);
}
}
void Order(int u) {
vector<pii> child;
for(int v: a[u]) child.pb(Min[v], v);
sort(child.begin(), child.end());
for(pii p: child) Order(p.se);
order[u] = ++cur;
if(deg[u] == 0) leaf.insert(mp(order[u], u));
}
int Node(int x, int step) {
for(int i = logN; i >= 0; --i)
if(step >= (1 << i)) x = p[x][i], step -= (1 << i);
return x;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define Task "test"
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> p[i][0]; a[p[i][0]].pb(i);
if(!p[i][0]) root = i;
}
DFS(root); Order(root);
while(q --) {
cin >> x >> k;
if(x == 1) {
while(k --) {
x = (*leaf.begin()).se; leaf.erase(leaf.begin());
if(--deg[p[x][0]] == 0) leaf.insert(mp(order[p[x][0]], p[x][0]));
del[x] = 1;
}
cout << x << '\n';
} else {
x = k;
for(int i = logN; i >= 0; --i) {
if(del[p[x][i]]) x = p[x][i];
}
del[x] = 0, leaf.insert(mp(order[x], x));
if(++deg[p[x][0]] == 1) leaf.erase(mp(order[p[x][0]], p[x][0]));
cout << d[k] - d[x] << '\n';
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int32_t main()':
ballmachine.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |