이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//i_love_aisukiuwu
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/home/trucmai/.vim/tools.h"
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define debug(x...)
#endif
void open(){
if(fopen("i.inp","r")){
freopen("i.inp","r",stdin);
freopen("o.out","w",stdout);
}
}
#define ll long long
#define all(a) (a).begin(), (a).end()
#define vi vector<ll>
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define fi first
#define se second
#define gcd __gcd
#define mset(a,v) memset(a, v, sizeof(a))
#define endl '\n'
#define spc " "
const int MN1 = 1e5 + 5,MN2 = 1e4 + 5,LOG = 27;
const ll MOD = 1e9 + 7,INF = 1e9;
ll n,q,p[MN1],root;
ll mx[MN1],h[MN1],tin[MN1];
ll up[MN1][LOG];
bool col[MN1];
vector<ll>adj[MN1];
priority_queue<pi>pq;
void dfs(ll u,ll pre){
mx[u] = u;
for(ll v : adj[u]){
if(v == pre) continue;
h[v] = h[u] + 1;
up[v][0] = u;
for(ll i = 1;i < LOG;++i) up[v][i] = up[up[v][i-1]][i-1];
dfs(v,u);
mx[u] = min(mx[u],mx[v]);
}
}
ll timer = 0;
void dfs2(ll u,ll p){
for(ll v : adj[u])
if(v != p) dfs2(v,u);
tin[u] = ++timer;
}
void solve(){
cin>>n>>q;
for(ll i = 1;i <= n;++i){
cin>>p[i];
if(p[i] == 0){root = i; continue;}
adj[p[i]].push_back(i);
}
dfs(root,0);
for(ll i = 1;i <= n;++i)
sort(all(adj[i]),[&](ll x,ll y){return mx[x] < mx[y];});
dfs2(root,0);
// for(ll i = 1;i <= n;++i)
// cout<<"DEBUG: "<<i<<spc<<tin[i]<<endl;
for(ll i = 1;i <= n;++i) pq.push({tin[i],i});
while(q--){
ll t,u; cin>>t>>u;
if(t == 1){
ll v = 0;
while(u--){
v = pq.top().fi; pq.pop();
// cout<<"DEBUG: "<<v<<endl;
col[v] = true;
}
cout<<v<<endl;
}
else{
ll v = u;
for(ll i = LOG;i >= 0;--i)
if(col[up[v][i]]) v = up[v][i];
col[v] = false;
pq.push({tin[v],v});
cout<<h[u] - h[v]<<endl;
}
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
open();
ll t = 1; //cin>>t;
while(t--) solve();
cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'void open()':
ballmachine.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | freopen("i.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
ballmachine.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("o.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... |