This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 110000
ii n, q, a[maxn], mn[maxn], ant[maxn], timer, p[maxn][25], h[maxn];
vector<ii> g[maxn];
bool cmp(ii a, ii b) {
return mn[a] < mn[b];
}
void dfsmn(ii u, ii ant) {
//cout << u << " " << ant << endl;
p[u][0] = ant;
//cout << u << " " << 0 << " = " << p[u][0] << endl;
for(ii i = 1; i <= 20; i++) {
p[u][i] = p[p[u][i-1]][i-1];
//cout << u << " " << i << " = " << p[u][i] << endl;
}
for(auto v : g[u]) {
h[v] = h[u]+1;
dfsmn(v,u);
mn[u] = min(mn[u],mn[v]);
}
}
void dfs(ii u, ii ant) {
for(auto v : g[u]) {
dfs(v,u);
}
a[u] = ++timer;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
cin >> n >> q;
ii root;
for(ii i = 1; i <= n; i++) {
mn[i] = i;
ii v;
cin >> v;
if(v == 0) root = i;
else g[v].pb(i);
}
dfsmn(root,root);
for(ii i = 1; i <= n; i++) {
sort(all(g[i]),cmp);
}
dfs(root,root);
set<pair<ii,ii>> s;
for(ii i = 1; i <= n; i++) {
s.insert(mp(a[i],i));
}
for(ii i = 1; i <= q; i++) {
ii op;
cin >> op;
if(op == 1) {
ii k; cin >> k;
ii ans;
while(k--) {
ans = s.begin()->sc;
s.erase(*s.begin());
}
cout << ans << endl;
}
else {
ii u; cin >> u;
ii ans = h[u];
for(ii i = 20; i >= 0; i--) {
ii v = p[u][i];
if(s.find(mp(a[v],v)) == s.end()) {
u = v;
}
}
ans-= h[u];
s.insert(mp(a[u],u));
cout << ans << endl;
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:10:14: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
10 | #define endl '\n'
| ^~~~
ballmachine.cpp:89:16: note: 'ans' was declared here
89 | ii ans;
| ^~~
ballmachine.cpp:75:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | dfs(root,root);
| ~~~^~~~~~~~~~~
# | 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... |