이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector< vector<int> > Graph;
vector<int> minLeft;
vector<int> minRight;
const int INF = 1e15;
void dfs(int x, int last){
for(int a : Graph[x]){
if(a == last) continue;
dfs(a,x);
}
if(!Graph[x].size()){
minLeft[x] = INF;
minRight[x] = INF;
return;
}
minLeft[x] = min(min(minLeft[Graph[x][0]], minRight[Graph[x][0]]), Graph[x][0]);
minRight[x] = min(min(minRight[Graph[x][1]], minLeft[Graph[x][1]]), Graph[x][1]);
}
vector< bool > isFull;
int last;
void add(int node){
// cout << node << endl;
if(Graph[node].size() == 0 || (isFull[Graph[node][0]] && isFull[Graph[node][1]])){
isFull[node] = true;
last = node;
return;
}
if(isFull[Graph[node][1]]) add(Graph[node][0]);
else if(isFull[Graph[node][0]]) add(Graph[node][1]);
else if(minLeft[node] < minRight[node]){
add(Graph[node][0]);
}else{
add(Graph[node][1]);
}
}
vector< int > up;
int tot ;
void rem(int node, int last){
if(node == 0) return;
if(last != -1){
if(!isFull[last] && isFull[node]){
tot++;
isFull[last] = true;
isFull[node] = false;
}
}
rem(up[node], node);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
Graph.resize(n+1);
int root;
up.resize(n+1);
for(int i = 0; i < n; i++){
int a;
cin >> a;
Graph[a].push_back(i+1);
up[i+1] = a;
if(a == 0) root = i+1;
}
minRight.resize(n+1);
minLeft.resize(n+1);
dfs(root,-1);
isFull.resize(n+1);
while(q--){
int a;
cin >> a;
int b;
cin >> b;
if(a == 1){
for(int i = 0; i < b; i++){
add(root);
}
cout << last << endl;
}else{
isFull[b] = false;
tot = 0;
rem(b,-1);
cout << tot << endl;
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:93:20: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
add(root);
~~~^~~~~~
# | 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... |