Written by
nodejs-style
on
on
백준 알고리즘 3182번 한동이는 공부가 하기 싫어!(python)
백준 알고리즘 3182번 한동이는 공부가 하기 싫어!(python)
728x90
기본적인 그래프 탐색 문제이다.
각 노드별로 몇 명의 사람을 만날 수 있는지 확인하고, 최댓값의 인덱스를 출력해주면 된다 -> res.index(max(res))
def dfs(node, cnt): check[node] = 1 n = graph[node][0] if check[n] == 0: cnt = dfs(n, cnt+1) return cnt N = int(input()) graph = [[] for _ in range(N+1)] res = [0]*(N+1) for u in range(1, N+1): v = int(input()) graph[u].append(v) for i in range(1, N+1): check = [0]*(N+1) res[i] = dfs(i, 1) print(res.index(max(res)))
반응형
from http://jinho-study.tistory.com/1096 by ccl(A) rewrite - 2021-08-10 21:00:17