Submission #102310

#TimeUsernameProblemLanguageResultExecution timeMemory
102310rubenvdBall Machine (BOI13_ballmachine)C++14
40.71 / 100
1087 ms11576 KiB
#include <bits/stdc++.h> using namespace std; int par[100005], val[100005]; bool filled[100005]; vector<int> child[100005]; bool compareInt(const int& lhs, const int& rhs) { return val[lhs] < val[rhs]; } int dfs( int u ){ int mini = 1e9; for( auto v : child[u] ){ mini = min(dfs(v), mini); } val[u] = min(u, mini); return val[u]; } pair<int, int> add( int u, int cnt ){ //cout << u << " " << cnt << endl; bool used = false; pair<int, int> ret; for( auto v : child[u] ){ used = true; if( !filled[v] && cnt > 0 ){ ret = add(v, cnt); cnt = ret.second; } } if( !used || cnt > 0 ){ filled[u] = true; //cout << " " << u << " " << cnt-1 << endl; return {u, cnt-1}; } else{ //cout << " " << ret.first << " " << cnt << endl; return {ret.first, cnt}; } } int del( int u ){ int in = par[u], cnt = 0, prev = u; while( filled[in] ){ prev = par[prev]; cnt++; in = par[in]; } filled[prev] = false; return cnt; } int main(){ int n, q, root, type, k; cin >> n >> q; memset(filled, false, sizeof filled); for( int i = 1; i <= n; ++i ){ cin >> par[i]; child[par[i]].push_back(i); if( par[i] == 0 ) root = i; } dfs(root); for( int i = 0; i < n; ++i ) sort(child[i].begin(), child[i].end(), compareInt); for( int i= 0; i < q; ++i ){ cin >> type >> k; if( type == 1 ){ pair<int, int> out; out = add(root, k); cout << out.first << endl; } else { cout << del(k) << endl; } } return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:78:13: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
    out = add(root, k);
          ~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...