Submission #897279

#TimeUsernameProblemLanguageResultExecution timeMemory
897279MathiasBall Machine (BOI13_ballmachine)C++14
100 / 100
155 ms31152 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5+7; const int LOG = 19; int jp[MAXN][LOG]; int ojciec[MAXN]; int depth[MAXN]; int mini[MAXN]; int post[MAXN]; bool ball[MAXN]; vector<int> g[MAXN]; set<pair<int,int> > s; int cnt=0; void DFS(int x){ depth[x]=depth[ojciec[x]]+1; jp[x][0]=ojciec[x]; for(int i=1;i<LOG;i++) jp[x][i]=jp[jp[x][i-1]][i-1]; vector<pair<int,int> > vec; for(int i=0;i<g[x].size();i++) vec.push_back({mini[g[x][i]], g[x][i]}); sort(vec.begin(),vec.end()); for(int i=0;i<vec.size();i++) DFS(vec[i].second); //cout<<"x="<<x<<" cnt+1="<<cnt+1<<'\n'; s.insert({++cnt,x}); post[x]=cnt; } void DFS1(int x){ for(int i=0;i<g[x].size();i++){ DFS1(g[x][i]); } mini[x]=x; for(int i=0;i<g[x].size();i++) mini[x]=min(mini[x],mini[g[x][i]]); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,q,x,r,a,b; pair<int,int> p; cin>>n>>q; for(int i=1;i<=n;i++){ cin>>x; ojciec[i]=x; if(x==0) r=i; else g[x].push_back(i); } ojciec[r]=r; DFS1(r); DFS(r); //for(int i=1;i<=n;i++) cout<<mini[i]<<' '; cout<<'\n'; while(q--){ cin>>a>>b; if(a==1){ while(b--){ auto it=s.begin(); p=*it; //cout<<b<<' '<<p.first<<' '<<p.second<<'\n'; ball[p.second]=1; if(b==0){ cout<<p.second<<'\n'; } s.erase(it); } } else{ x=b; for(int i=LOG-1;i>=0;i--){ if(ball[jp[x][i]]==1){ x=jp[x][i]; } } ball[x]=0; s.insert({post[x],x}); //cout<<b<<' '<<x<<'\n'; cout<<depth[b]-depth[x]<<'\n'; } //for(int i=1;i<=n;i++){ // cout<<ball[i]<<' '; //} //cout<<'\n'; //for (auto it = s.begin(); it != s.end(); it++){ // p=*it; // cout<<p.second<<' '; //} //cout<<'\n'; } return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'void DFS(int)':
ballmachine.cpp:19:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |  for(int i=0;i<g[x].size();i++) vec.push_back({mini[g[x][i]], g[x][i]});
      |              ~^~~~~~~~~~~~
ballmachine.cpp:21:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for(int i=0;i<vec.size();i++) DFS(vec[i].second);
      |              ~^~~~~~~~~~~
ballmachine.cpp: In function 'void DFS1(int)':
ballmachine.cpp:27:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |  for(int i=0;i<g[x].size();i++){
      |              ~^~~~~~~~~~~~
ballmachine.cpp:31:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |  for(int i=0;i<g[x].size();i++) mini[x]=min(mini[x],mini[g[x][i]]);
      |              ~^~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:46:5: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |  DFS(r);
      |  ~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...