[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๊ฐ์ฅ ๋จผ ๋
ธ๋ / Python
๐ Problem Solving/Programmers
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๊ฐ์ฅ ๋จผ ๋ ธ๋
6 [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] 3
programmers.co.kr


import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
vertex = [list(map(int, input().split())) for _ in range(int(input()))]
def getDistance(start, graph, distance):
queue = deque()
queue.append(start)
while queue:
x = queue.popleft()
for i in graph[x]:
if i == 1:
continue
if distance[i] == 0:
distance[i] += distance[x] + 1
queue.append(i)
return distance
def solution(n, vertex):
graph = [[] for _ in range(n + 1)]
distance = [0] * (n + 1)
for a, b in vertex:
graph[a].append(b)
graph[b].append(a)
temp = getDistance(1, graph, distance)
return temp.count(max(temp))
print(solution(n, vertex))
ํด์ค
์ด๋ ต์ง ์๊ฒ ํด๊ฒฐํ ์ ์์๋ ๊ทธ๋ํ ๋ฌธ์ ์๋ค.๐
๊ฐ ์ ์ ๋ณ๋ก ๊ฑฐ๋ฆฌ 1๋ก ์ด์ด์ง๋ ์ ์ ๋ค์ ์ ์ฅํ๋ค.
์์ ์ ์ 1์ deque์ ์ ์ฅํ๋ค. while๋ฌธ ์์์ ํด๋น ์ ์ ์ deque์์ ๋นผ๋ด๊ณ ๊ทธ ์ ์ ์ผ๋ก๋ถํฐ ์ด์ด์ง ์ ์ ๋ค๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ค. ์ด ๋, ํ์ํ์ง ์์ ์ ์ ๋ค๋ง ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ด ์ค์!! ๊ฑฐ๋ฆฌ ๊ณ์ฐ์ด ์๋ฃ๋ ์ ์ ์ deque์ ์ ์ฅํ๊ณ ์ด๋ฅผ ๋ฐ๋ณตํ๋ค.
'๐ Problem Solving > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋ฑ๊ตฃ๊ธธ / Python (0) | 2021.06.18 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ์ด์ค์ฐ์ ์์ํ / Python (0) | 2021.06.18 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋คํธ์ํฌ / Python (0) | 2021.06.17 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋์คํฌ ์ปจํธ๋กค๋ฌ / Python (0) | 2021.06.17 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋ฒ ์คํธ์จ๋ฒ / Python (0) | 2021.06.16 |