본문 바로가기
알고리즘/프로그래머스 level 3

[파이썬🐍] 프로그래머스 : 가장 먼 노드

by 코딩개미뚠뚠 2021. 6. 30.
반응형
from collections import deque

def solution(n, edge):
    def bfs():
        q = deque()
        q.append(1)

        while q:
            x = q.popleft()
            for i in a[x]:
                if ch[i] == 0:
                    ch[i] = ch[x] + 1
                    q.append(i)
                
    a = [[] for i in range(n+1)]
    ch = [0]*(n+1)
    for i,j in edge:
        a[i].append(j)
        a[j].append(i)
    ch[1] = 1
    bfs()
    return ch.count(max(ch))

<bfs 풀이>

 

반응형

댓글