[20250727] BOJ / P5 / 내 왼손에는 흑염룡이 잠들어 있다 / 권혁준 #557
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/13016
🧭 풀이 시간
15분
👀 체감 난이도
✏️ 문제 설명
정점 N개인 트리의 각 점에 대해, 그 점과 가장 먼 점까지의 거리를 구해보자.
🔍 풀이 방법
일단 1번 점을 루트로 잡고, 두 가지 dp를 정의한다.
down[n] = n번 점을 루트로 하는 서브트리 내에서 n과 가장 먼 거리
up[n] = n번 점을 루트로 하는 서브트리 외에서 n과 가장 먼 거리
down[n]은 n의 자식들로 가는 간선 (n, i, c) 에 대해, max(down[i] + c)로 쉽게 구할 수 있다.
-> dfs를 이용하면 O(N)에 가능
up[n]은 n의 부모로 가는 간선 (p, n, c)에 대해, up[p] + c이거나,
p의 자식들로 가는 간선 (p, i, c)에 대해, max(down[p] + c)가 된다.
-> 이 때, i가 n이 되는 경우는 제외해야 하는 것에 주의하며 구현한다.
⏳ 회고
ez