目录
题目描述
树形目录,m行输入,[当前目录ID 父级目录ID]。最后一行为待删除目录ID,要求删除父级同时删除下级目录。 输出剩余目录。
参考示例
示例1
输入:
6
3 1
5 1
4 3
10 5
11 5
12 4
5
输出:
3 1
4 3
12 4
参考代码
def calc():
global lst, tree, del_child
num_input = int(input())
lst = []
tree = {}
for _ in range(num_input):
child, parent = input().split()
if parent in tree:
tree[parent].append(child)
else:
tree[parent] = [child]
lst.append((child, parent))
def del_child(parent_id):
if parent_id in tree:
for child_id in tree.pop(parent_id):
lst.remove((child_id, pare