이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
const int mod = 1e9 +7, sze = 1e5 +10, inf = LLONG_MAX, LG = 20;
vector<int> graph[sze];
int mn[sze];
int depth[sze];
set<pair<int,int>> bos;
int lan[sze];
int pp[sze];
int dd[sze];
int up[LG][sze];
void dfs(int node,int par=0){
mn[node]=node;
pp[node]=par;
up[0][node]=par;
for(int i=1;i<LG;i++){
up[i][node]=up[i-1][up[i-1][node]];
}
for(auto v:graph[node]){
if(v!=par){
depth[v]=depth[node]+1;
dfs(v,node);
mn[node]=min(mn[node],mn[v]);
// deg++;
dd[node]++;
}
}
if(dd[node]==0){
bos.ins({mn[node],node});
}
}
void rush(){
int n,q;
cin>>n>>q;
int root=0;
for(int i=1;i<=n;i++){
int x;cin>>x;
if(x){
graph[i].pb(x);
graph[x].pb(i);
}
else{
root=i;
}
}
dfs(root,0);
while(q--){
int op;
int node;
cin>>op>>node;
if(op==1){
int last= 0;
while(node--){
if(bos.empty()){
// putr("bitdi");
assert(0);
}
auto yalvar = *bos.begin();
bos.erase(bos.begin());
lan[yalvar.second]=1;
last= yalvar.second;
// cout<<last<<" "<<pp[last]<<endl;
if(pp[last]){
--dd[pp[last]];
if(dd[pp[last]]==0){
bos.insert({mn[pp[last]],pp[last]});
}
}
}
cout<<last<<endl;
}
else{
int emp = node;
for(int i= LG-1;i>=0;i--){
if(lan[up[i][emp]]){
emp = up[i][emp];
}
}
lan[emp]=0;
int surus = depth[node] - depth[emp];
cout<<surus<<endl;
if(pp[emp]){
dd[pp[emp]]++;
}
bos.insert({mn[emp],emp});
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
rush();
}
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... |