node = int(input())
parent = [int(x) for x in input().split(' ')]
deleted = int(input())
class Node :
def __init__(self, data):
self.data = data
self.child = []
class Tree :
def __init__(self, node):
self.root = node
self.leaf = 0
def preorder(self, node):
for i in node.child :
if i.data == deleted:
continue
self.preorder(i)
if not node.child or (len(node.child) == 1 and node.child[0].data == deleted):
self.leaf += 1
nodeList = []
for i in range(node) :
nodeList.append(Node(i))
for i in range(node) :
if parent[i] == -1 :
myTree = Tree(nodeList[i])
continue
nodeList[parent[i]].child.append(nodeList[i])
if myTree.root.data == deleted :
print(0)
else :
myTree.preorder(myTree.root)
print(myTree.leaf)
'컴퓨터 > 코테' 카테고리의 다른 글
릿코드62 - Unique paths [dp] (0) | 2021.08.07 |
---|---|
릿코드155 - min Stack [스택] (0) | 2021.08.07 |
릿코드 2 - 두 수 더하기 (리스트) (0) | 2021.08.05 |
릿코드 20 - 괄호 짝 맞추기 (0) | 2021.08.05 |
릿코드 104 - 트리의 가장 깊은 층 (0) | 2021.08.04 |