전반적으로 find-union 형식으로 구현해주었다
graph는 child를 key로, parent를 value로 설정하여 dictionary로 선언해주었고, all_profit 역시 dictionary로 선언해주었다
distribute(node, profit) 함수에서 재귀 호출을 할 때, TC 11번 ~ 13번이 런타임 에러가 났는데, recursion limit exceeded가 뜬 것으로 추정된다
def solution(enroll, referral, seller, amount):
N = len(enroll)
graph = dict() #key:child, value:parent
for i in range(N): graph[enroll[i]] = referral[i]
all_profit = dict()
for i in range(N): all_profit[enroll[i]] = 0
def distribute(node, profit):
if profit // 10 == 0:
all_profit[node] += profit
return
tax = profit // 10
all_profit[node] += profit - tax
if graph[node] != '-':
distribute(graph[node], tax)
M = len(seller)
for i in range(M): distribute(seller[i], amount[i]*100)
return list(all_profit.values())