[20250311] BOJ / P5 / 등산 마니아 / 권혁준 #225
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/20188
🧭 풀이 시간
40분
👀 체감 난이도
✏️ 문제 설명
N개의 오두막과 N-1개의 오솔길이 트리 모양으로 등산로를 이루고 있다.
산 정상에는 1번 오두막이 있고, 한 오두막에서 다른 오두막으로 갈 때는 항상 산 정상을 거치는 가장 짧은 길로 간다.
이 때 길의 다양성은 길에 포함된 오솔길의 개수다. (거리를 구하는 게 아님!! 지나온 길의 개수를 구하는거)
가능한 모든 조합(a,b)에 대해, a번 오두막에서 b번 오두막으로 가는 길의 다양성의 총 합을 구해보자.
🔍 풀이 방법
[사용한 알고리즘]
산 정상을 찍지 않고 최단 거리로만 간다고 가정을 하면, 간단한 식으로 해결할 수 있다.$sub[n] \times (N - sub[n])$ 이다.
n을 루트로 하는 서브트리의 크기를 sub[n]이라 할 때, 답은
이 값에 산 정상을 거치는 경우를 더해서 답을 구할 수 있다.$sub[n] - 1 + \sum {sub[i] * (sub[n] - sub[i] - 1)}$ 이다.
1~n까지의 경로가 겹치게 되는 경우의 수는, n의 자식들 i에 대해,
여기에 경로의 길이를 곱해주면 된다.
⏳ 회고
쉬워보였는데 식 찾기가 생각보다 너무 힘들었다.