[λ°±μ€-6118] μ¨λ°κΌμ§ / Python
π Problem Solving/Baekjoon
6118λ²: μ¨λ°κΌμ§
μ¬μκΈ°λ μνλμ κ΅μΈ λμ₯μμ μ¨λ°κΌμ§μ νκ³ μλ€. λμ₯μλ νκ°μ΄ λ§μ΄ λλ €μκ³ μ¬μκΈ°λ κ·Έ μ€μ νλμ μ¨μ΄μΌ νλ€. νκ°μ κ°μλ N(2 <= N <= 20,000)κ°μ΄λ©°, 1 λΆν° μλ€κ³ νμ. μ¬
www.acmicpc.net
import sys
import heapq
input = sys.stdin.readline
INF = int(1e9)
n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
distance = [-1] * (n + 1)
def dijkstra(start):
q = []
heapq.heappush(q, (0, start))
distance[start] = 0
while q:
dist, now = heapq.heappop(q)
for i in graph[now]:
if distance[i] == -1:
distance[i] = dist + 1
heapq.heappush(q, (distance[i], i))
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
dijkstra(1)
print(distance.index(max(distance)), max(distance), distance.count(max(distance)))
ν΄μ€
λ€μ΅μ€νΈλΌλ‘ ν΄κ²°νλ€. μ΄λ ΅μ§ μμ λ¬Έμ μλλ° μμ μ μ μ 1λ‘ μ€μ μ ν΄μ 100% νλ Έμ΅λλ€κ° λ΄λ€..π μμ μ μ μ 0μΌλ‘ μ€μ ν΄μ€μΌ νλ€.
'π Problem Solving > Baekjoon' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€-5972] νλ°° λ°°μ‘ / Python (0) | 2021.07.02 |
---|---|
[λ°±μ€-18405] κ²½μμ μ μΌ / Python (0) | 2021.07.02 |
[λ°±μ€-1743] μμλ¬Ό νΌνκΈ° / Python (0) | 2021.07.02 |
[λ°±μ€-1926] κ·Έλ¦Ό / Python (0) | 2021.07.01 |
[λ°±μ€-2251] λ¬Όν΅ / Python (0) | 2021.07.01 |