<오답>
from collections import deque
n = int(input())
graph = [[] for _ in range(n+1)]
score = [0] * (n+1)
while True:
a,b = map(int,input().split())
if a == -1 and b == -1:
break
graph[a].append(b)
graph[b].append(a)
def bfs(start):
q = deque()
q.append(start)
while q:
now = q.popleft()
for i in graph[now]:
if score[i] == 0:
score[i] = score[now] + 1
q.append(i)
bfs(1)
min_score = min(score)
print(min_score,score.count(min_score))
print(score.index(min_score))
<정답>
import sys
from collections import deque
input = sys.stdin.readline
q = deque()
n = int(input())
graph = [[] for _ in range(n+1)]
while True:
a,b = map(int,input().split())
if a == -1 and b == -1:
break
graph[a].append(b)
graph[b].append(a)
def bfs(now):
visited[now] = True
q.append(now)
while q:
x = q.popleft()
for i in graph[x]:
if not visited[i]: #방문하지 않았다면
q.append(i) #다음 탐색을 위해 append 시켜준 뒤
score[i] = score[x] + 1 #현재 점수 + 1 해준다.
visited[i] = True #방문처리
res = []
limit = 51
for i in range(1, n+1):
score = [0] * (n+1)
visited = [False] * (n+1)
bfs(i)
m = max(score)
if m == limit:
res.append(i)
elif m < limit:m
res = [i]
limit = m
print(limit, len(res))
print(*res)
추후
Floyd-Warshall란?
'문제풀이 > 백준(Boj) 문제풀이' 카테고리의 다른 글
[백준][Dijkstra/다익스트라] 4885. 녹색 옷 입은 애가 젤다지? (파이썬/Python) (0) | 2021.12.29 |
---|---|
[백준][DFS/BFS] 21736. 헌내기는 친구가 필요해 (파이썬/Python) (0) | 2021.12.28 |
[백준][그리디 알고리즘] 20044. Project Teams(파이썬/Python) (0) | 2021.12.27 |
[백준][Greedy/BFS] 16953. A → B (파이썬/Python) (0) | 2021.12.27 |
[백준][DFS/BFS]2644. 촌수계산 (파이썬/Python) (0) | 2021.12.26 |