#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
 
vector<int> graph[55];
int N;
int rootNode;
int delNode;
int leafCnt;
 
void find_leaf(int node)
{
    if(graph[node].size() == 0)
    {
        leafCnt++;
    }
    
    for(int i = 0; i < graph[node].size(); i++)
    {
        // 현재노드의 자식이 삭제할 노드이고, 
        if(graph[node][i] == delNode)
        {
            // 현재노드의 자식이 1개 밖에 없으면, 현재 노드의 자식을 삭제할 경우 그 현재 노드가 리프노드가 됨 
            if(graph[node].size() == 1)
            {
                leafCnt++;    
            }
            
            continue;
        }
        
        find_leaf(graph[node][i]);
    }
}
 
int main(void)
{
//    freopen("B1068_input.txt", "r", stdin);
    
    cin >> N;
    
    for(int to = 0; to < N; to++)
    {
        int from;
        cin >> from;
        
        if(from == -1)
        {
            rootNode = to;
            
            continue;
        }
        else
        {
            graph[from].push_back(to);    
        }
    }
    
    cin >> delNode;
    
    if(delNode == rootNode)
    {
        cout << "0";
    }
    else
    {
        find_leaf(rootNode);
        cout << leafCnt;    
    }
    
    return 0;
}
cs

+ Recent posts