이거 위상정렬에 inDegree 써서 풀었었던데 난 그런건 기억 안나고 쉽게 풀 수 있는거같아서 이렇게 풀었다. 예외 case들까지 생각해서 틀어막앗는데 계속 틀렷다고 나왔다. 반례좀 올려달라고 질문에 올려놨는데 올려줄 지 모르겠다. 조로 피규어 갖고싶다.

import sys
sys.stdin = open('input.txt')
input = sys.stdin.readline

N = int(input())
n_lines = int(input())

components = []
for _ in range(n_lines):
    components.append(list(map(int, input().split())))

# 첫번째 원소 내림차순 그다음에 두번째 원소 내림차순 정렬
components.sort(key=lambda x:(x[0], x[1]), reverse = True)
# big_component = components[-1][0]

stack = [0] * (N+1)
stack[-1] = 1

# 중간 부품을 담는 list. 부품 6이라 해도 6을 만드는데 기본부품이 필요없으면 6이 기본 부품인거다
big_component = []

for i in range(n_lines):
    bigger, smaller, how_many = components[i]
    stack[smaller] += stack[bigger] * how_many
    big_component.append(bigger)
    
big_component = set(big_component)

for i in range(1, N):
    if i not in big_component and stack[i] != 0:
        print(i,stack[i])

Untitled

오우 간지 폭탄이다.