어..... 반례를 찾았다
직선이면 안되네
ex) 4 -1 0 1 2 2 넣으면 0 나오네요
단말노드 오류난다고 부모 제거하면 절대 안되요 ㅎㅎ
#include <iostream>
#include <vector>
#define MAX 50
using namespace std;
vector<int> adj[MAX];
bool check[MAX];
int n, root, d, cnt;
void DeleteTree(int root){
check[root] = false;
}
void SearchTree(int root){
if(check[root]==false){return;}
int lll=0;
for(int i = 0 ; i < adj[root].size(); i++){
int child = adj[root][i];
if(check[child] == true){
lll++;
SearchTree(child);
}
}
if(lll==0){
cnt++;
}
}
int main(){
cin >> n;
for(int i = 0 ; i < n ; i++){
check[i] = true;
int tmp;
cin >> tmp;
if(tmp == -1) root = i;
else{
adj[tmp].push_back(i);
}
}
cin >> d;
DeleteTree(d);
SearchTree(root);
cout << cnt << endl;
return 0;
}
yjy1129 6년 전