This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 450;
const int INF = 0x3f3f3f3f;
int n, q, filled[MAX], add, root, ans;
vector<int> adj[MAX];
void dfs(int u, int p){
for(auto v : adj[u]){
if(v == p) continue;
if(!filled[v]) dfs(v, u);
}
if(add && !filled[u]) filled[u] = true, ans = u, add--;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for(int i = 1; i <= n; i++){
int x; cin >> x;
if(x != 0){
adj[x].push_back(i);
adj[i].push_back(x);
}else{
root = i;
}
}
for(int i = 1; i <= n; i++)
sort(adj[i].begin(), adj[i].end());
while(q--){
int op, k; cin >> op >> k;
if(op == 1){
add = k;
ans = root;
dfs(root, -1);
cout << ans << '\n';
}else{
cout << rand() << '\n';
}
}
return 0;
}
# | 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... |