반응형
https://www.acmicpc.net/problem/11403
import collections
n = int(input())
graph = collections.defaultdict(list)
for i in range(n):
edges = list(map(int, input().split()))
for j in range(len(edges)):
if edges[j] == 1:
graph[i].append(j)
def dfs(node):
if node in discovered:
return
discovered.append(node)
for next in graph[node]:
dfs(next)
for i in range(n):
discovered = []
for j in graph[i]:
dfs(j)
answer = [0]*n
for j in range(len(discovered)):
answer[discovered[j]] = 1
print(*answer)
쉬운 dfs 문제!
댓글