백준-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)
'알고리즘 > 백준' 카테고리의 다른 글
백준-2992번(크면서 작은 수)-python (0) | 2023.11.23 |
---|---|
백준-10866번(덱)-python (0) | 2023.11.23 |
백준-24444번(알고리즘 수업 - 너비 우선 탐색 1)-python (0) | 2023.11.23 |
백준-24480번(알고리즘 수업 - 깊이 우선 탐색 2)-python (0) | 2023.11.23 |
백준-24479번(알고리즘 수업 - 깊이 우선 탐색 1)-python (0) | 2023.11.23 |