이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
const int MXN = 100005, LG = 18;
int n, q, rt;
vector<pii> chd[MXN];
int d[MXN], dfn[MXN], scr[MXN], sml[MXN], C;
int p[LG][MXN];
bitset<MXN> b;
priority_queue<int, vector<int>, greater<int>> pq;
void DFS(int id, int par, int dep) {
p[0][id] = par;
d[id] = dep;
sml[id] = id;
for (auto &[e, i] : chd[id]) {
DFS(i, id, dep + 1);
sml[id] = min(sml[id], sml[i]);
}
}
void DFS2(int id) {
for (auto &[e, i] : chd[id]) e = sml[i];
sort(chd[id].begin(), chd[id].end());
for (auto &[e, i] : chd[id]) DFS2(i);
scr[C] = id;
dfn[id] = C++;
}
int calc1(int x) {
int ans = 0;
while (x--) {
int id = pq.top();
pq.pop();
b[scr[id]] = true;
ans = scr[id];
}
return ans;
}
int calc2(int x) {
int x0 = x;
for (int w = LG - 1; w >= 0; w--) {
if (!b[p[w][x]]) continue;
x = p[w][x];
}
b[x] = false;
pq.push(dfn[x]);
return d[x0] - d[x];
}
void miku() {
int x, y;
cin >> n >> q;
FOR(i, 1, n + 1) {
cin >> x;
if (x == 0) rt = i;
else chd[x].push_back(mp(0, i));
}
DFS(rt, 0, 0);
DFS2(rt);
FOR(w, 1, LG) {
FOR(i, 1, n + 1) p[w][i] = p[w - 1][p[w - 1][i]];
}
FOR(i, 0, n) pq.push(i);
while (q--) {
cin >> x >> y;
if (x == 1) cout << calc1(y) << '\n';
else cout << calc2(y) << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
# | 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... |