# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
102312 | janlivens | Ball Machine (BOI13_ballmachine) | C++14 | 1082 ms | 9720 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// This Program is made by Jan(Codezebra)
#include<bits/stdc++.h>
//#define int long long
//#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
int n,q;
int root;
vector<bool> vol;
vector<int>parent;
vector<vector<pair<int,int> > >children;
//vector<int>m;
int find(int x){
for(int i=0;i<children[parent[x]].size();i++){
if(children[parent[x]][i].second==x){
return i;
}
}
return -1;
}
int build(int x){
// cout<<x<<" % ";
if(children[x].empty()){
//cout<<"0";
return children[parent[x]][find(x)].first=x;
}
int a=x;
for(auto e:children[x]){
a=min(a,build(e.second));
}
// cout<<a<< "\n";
return children[parent[x]][find(x)].first=a;
}
int roll(int x){
for(int i=children[x].size()-1;i>=0;i--){
if(!vol[children[x][i].second]){
return roll(children[x][i].second);
}
}
vol[x]=1;
return x;
}
int op1(int k){
for(int i=1;i<k;i++){
int qq=roll(1);
}
return roll(1);
}
int op2(int x){
int a=x;
int prev=x;
for(int i=0;;i++){
if(!vol[parent[a]]){
vol[a]=0;
return i;
}
a=parent[a];
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
children.resize(n+1);
parent.resize(n+1,0);
vol.resize(n+1,0);
for(int i=1;i<=n;i++){
int p;
cin>>p;
parent[i]=p;
children[p].push_back({0,i});
}
// root=children[0][0].second;
root=1;
// cout<<"input\n";
/* for(int e:parent){
cout<<e<<" ";
}cout<<"\n";
*/
build(root);
// cout<<"build\n";
for(int i=0;i<n;i++){
sort(children[i].begin(),children[i].end(), greater<pair<int,int> >());
}
for(int i=0;i<q;i++){
int type;
cin>>type;
if(type==1){
int k;
cin>>k;
cout<<op1(k)<<"\n";
}
else if(type==2){
int x;
cin>>x;
cout<<op2(x)<<"\n";
}
}
return 0;
}
Compilation message (stderr)
# | 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... |