Submission #989149

#TimeUsernameProblemLanguageResultExecution timeMemory
989149SuPythonyBall Machine (BOI13_ballmachine)C++17
60.08 / 100
1099 ms27984 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<vector<int>> al; vector<int> par; vector<int> mn; vector<int> ball; vector<int> order; set<pair<int,int>> next_node; int root,n; void dfs(int u) { for (int v: al[u]) { dfs(v); } order[u]=next_node.size()+1; next_node.insert({order[u],u}); } void mndfs(int u) { mn[u]=u; set<pair<int,int>> o; for (int v: al[u]) { mndfs(v); mn[u]=min(mn[u],mn[v]); o.insert({mn[v],v}); } al[u].clear(); for (auto i: o) { al[u].push_back(i.second); } } int add_ball() { auto b=*next_node.begin(); next_node.erase(next_node.begin()); ball[b.second]=1; return b.second; } int main() { int q; cin>>n>>q; al.assign(n+1,vector<int>()); par.assign(n+1,-1); mn.assign(n+1,1e9); ball.assign(n+1,0); order.assign(n+1,0); for (int i=1; i<=n; i++) { int p; cin>>p; if (p==0) { root=i; } else { al[p].push_back(i); par[i]=p; } } for (int i=1; i<=n; i++) { sort(al[i].begin(),al[i].end()); } mndfs(root); dfs(root); while (q--) { int t; cin>>t; if (t==1) { int k; cin>>k; int ans; for (int i=0; i<k; i++) { ans=add_ball(); } cout<<ans<<"\n"; } else { int x; cin>>x; int curr=x; int ans=0; while (par[curr]!=-1&&ball[par[curr]]) { curr=par[curr]; ans++; } next_node.insert({order[curr],curr}); ball[curr]=0; cout<<ans<<"\n"; } } return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:71:24: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   71 |             cout<<ans<<"\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...