Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- C언어 덱
- getline()함수
- string유형
- 괄호 검사 프로그램
- C언어 스택 연산
- 프로그래머스 배열만들기4
- 범위 기반 for문
- C언어 계산기 프로그램
- 유형 변환
- 주기억장치
- auto 키워드
- 문자형 배열
- LAN의 분류
- c언어 괄호검사
- 알고리즘 조건
- const l-value참조자
- l-value참조자
- 문제해결 단계
- r-value참조자
- 원형 연결 구조 연결된 큐
- 회전 및 자리 이동 연산
- 네트워크 결합
- 프로그래머스 푸드 파이트 대회
- 값/참조/주소에 의한 전달
- 운영체제 기능
- 논리 연산
- IPv4 주소체계
- const화
- 백준 파이썬
- 입출력 관리자
Archives
- Today
- Total
chyam
[프로그래머스 Lv3, python] - 가장 먼 노드 본문

https://school.programmers.co.kr/learn/courses/30/lessons/49189
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
# 내 풀이
from collections import defaultdict,deque
def solution(n, edge):
answer = 0
depth = defaultdict(int) # 깊이 : 몇개
depth[1] = 1
nodes = defaultdict(list)
for a, b in edge:
nodes[a].append(b)
nodes[b].append(a)
visited = [False]*(n+1)
visited[1] = True
queue = deque([(1,1)]) # 노드번호, depth
while queue:
node, dep = queue.popleft()
for x in nodes[node]:
if not visited[x]:
queue.append((x,dep+1))
depth[dep+1] += 1 # 해당 깊이 개수 + 1
visited[x] = True
return depth[len(depth)]
from collections import defaultdict, deque
def solution(n, edge):
nodes = defaultdict(list)
# 그래프 저장
for a, b in edge:
nodes[a].append(b)
nodes[b].append(a)
# 각 노드까지 거리 저장
depth = [-1] * (n + 1)
depth[1] = 0
queue = deque([1])
# BFS
while queue:
node = queue.popleft()
for x in nodes[node]:
if depth[x] == -1: # 방문 안 한 경우
depth[x] = depth[node] + 1
queue.append(x)
max_depth = max(depth)
return depth.count(max_depth)'프로그래머스 > LV3' 카테고리의 다른 글
| [프로그래머스 Lv3, python] - 경주로 건설 (0) | 2026.05.22 |
|---|---|
| [프로그래머스 Lv3, python] - 입국심사 (0) | 2026.05.13 |
| [프로그래머스 Lv3, python] - 섬 연결하기 (0) | 2026.05.11 |
| [프로그래머스 Lv3, python] - 여행경로 (0) | 2026.05.10 |
| [프로그래머스 Lv3, python] - 보석 쇼핑 (0) | 2026.05.08 |
