개발자 톡

연습문제 톡 나무 막대

고수분들 부탁드립니다. 더 이상 시간이 줄지 않네요 ㅠㅠ

등록일
2024-10-01 15:18:55
조회수
16
작성자
dudgns0908
import sys


N, L, R = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(N-1)]
branch_map = {}
for u, v, num in edges:
    if branch_map.get(u) is None:
        branch_map[u] = []
    
    branch_map[u].append((v, num))


def find_stick(node, unique_branch):
    count = 0
    current_num_stick = len(unique_branch)
    if L <= current_num_stick <= R:
        count += 1
    elif current_num_stick > R:
        return count
        
    if branch_map.get(node) is not None:        
        for child_node, num in branch_map[node]:
            if num in unique_branch:
                count += find_stick(child_node, unique_branch) 
            else:                    
                unique_branch.add(num)
                count += find_stick(child_node, unique_branch) 
                unique_branch.pop()
    
    return count


def main():
    count = 0
    for root_node in branch_map.keys():
        count += find_stick(root_node, unique_branch=set())


    print(count)


if __name__ == '__main__':
    main()
#나무_막대

이 카테고리의 톡 더보기