[๋ฐฑ์ค€-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์œผ๋กœ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.