Submission #943994

#TimeUsernameProblemLanguageResultExecution timeMemory
943994tamir1Ball Machine (BOI13_ballmachine)C++17
1.92 / 100
14 ms21960 KiB
#include<bits/stdc++.h> #define ff first #define ss second using namespace std; int root,n,q,i,j,type,x,a[100005],jump[20][100005],dist[100005]; vector<int> v[100005]; bitset<100005> ball; void dfs(int k){ a[k]=k; for(int i:v[k]){ dist[i]=dist[k]+1; dfs(i); a[k]=min(a[k],a[i]); } } int find(int x){ if(ball[jump[x][0]]==0) return x; return find(jump[x][0]); } int update(int x){ int mx=1e8,idx=0; for(int i:v[x]){ if(a[i]<mx && !ball[i]){ mx=a[i]; idx=i; } } if(idx!=0) return update(idx); else{ ball[x]=1; return x; } } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> q; for(i=1;i<=n;i++){ cin >> jump[i][0]; if(jump[i][0]==0) root=i; else v[jump[i][0]].push_back(i); } dfs(root); while(q--){ cin >> type >> x; if(type==1){ for(i=1;i<=x;i++){ type=update(root); } cout << type << "\n"; } else{ type=find(x); ball[type]=0; cout << dist[x]-dist[type] << "\n"; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...