본문 바로가기
알고리즘/백준

백준-24445번(알고리즘 수업 - 너비 우선 탐색 2)-python

by nyeongha 2023. 11. 23.

백준-24445번(알고리즘 수업 - 너비 우선 탐색 2)-python

24444번 문제에서

for x in adl:
    x.sort()

이 부분을

for x in adl:
    x.sort(reverse=True)

이렇게 변경해주면 되었다.

from collections import deque
import sys
input = sys.stdin.readline


V,E,R=map(int,input().split())
visited=[0]*(V+1)
adl=[[] for _ in range(V+1)]
cnt=1

for _ in range(E):
    m,n=map(int,input().split())
    adl[m].append(n)
    adl[n].append(m)

for x in adl:
    x.sort(reverse=True)    

def bfs(R):
    global cnt
    a=deque([R])
    visited[R]=cnt
    while a:
        b=a.popleft()
        for i in adl[b]:
            if visited[i]==0:
                cnt+=1
                visited[i]=cnt
                a.append(i)

bfs(R)
for k in visited[1:]:
    print(k)