#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 1e5+5;
const int B = 22;
vector<int> adj[N];
int sp[N][B], ad, A[N], mal[N], d[N], mn[N];
bool belki[N];
set<int> s;
void dfs(int x){
mn[x]=x;
tr(it, adj[x])
d[*it]=d[x]+1, dfs(*it), umin(mn[x], mn[*it]);
}
void dfs2(int x){
tr(it, adj[x])
dfs2(*it);
mal[x]=ad;
A[mal[x]]=x;
s.insert(mal[x]);
ad++;
}
int bok(int x){
for(int i = B-1; i >= 0; --i)
if(belki[sp[x][i]])
x=sp[x][i];
return x;
}
int main(){
// freopen("file.txt", "r", stdin);
int n, q;
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i){
int x;
scanf("%d", &x);
sp[i][0]=x;
if(x==0)
ad=i;
else
adj[x].pb(i);
}
dfs(ad);
for(int j = 1; j < B; ++j)
for(int i = 1; i <= n; ++i)
sp[i][j]=sp[sp[i][j-1]][j-1];
auto cmp = [&](int i, int j) -> bool {
return mn[i]<mn[j];
};
for(int i = 1; i <= n; ++i)
sort(all(adj[i]), cmp);
dfs2(ad);
while(q--){
int op, x;
scanf("%d%d", &op, &x);
if(op==1){
int answer = -1;
while(x--){
auto ii = s.begin();
answer=A[*ii];
belki[answer]=1;
s.erase(ii);
}
printf("%d\n", answer);
continue;
}
int samsyk = bok(x);
belki[samsyk]=0;
s.insert(mal[samsyk]);
printf("%d\n", d[x]-d[samsyk]);
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
ballmachine.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
ballmachine.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d", &op, &x);
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |