[20250813] BOJ / D5 / Cat Exercise / 권혁준#660
Merged
ShinHeeEul merged 1 commit intomainfrom Aug 13, 2025
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/27539
🧭 풀이 시간
40분
👀 체감 난이도
✏️ 문제 설명
N개의 캣타워가 트리 형태로 주어진다.
캣타워들은 각각 높이가 1부터 N까지 겹치지 않게 구성되어 있다.
처음에, 고양이 한 마리가 높이 N인 캣타워에 있다.
캣타워에 장애물을 하나씩 놓을 수 있으며, 장애물의 위치에 따라 두 가지 경우로 나뉜다.
장애물을 잘 놓아서 고양이의 이동 거리를 최대로 해보자.
🔍 풀이 방법
s[u] = u에서 출발했을 때의 최대 이동 거리,
r[u] = u가 속한 컴포넌트에서의 최대 높이
라고 해보자.
낮은 곳부터 처리해야 함은 직관적으로 알 수 있다. -> 간선을 max(u,v)가 작은 순으로 정렬시키고 생각하자.
간선들을 정렬시키고 나면, 간선 (u, v) 에 대해 (u < v 라고 가정)
s[v] = max(s[v], s[r[u]] + dist(r[u], v)) 가 성립하게 된다.
여기서 dist는 트리 상에서 두 점 사이의 거리를 의미하며, 트리를 희소 배열로 전처리 해두면 한 번 당 O(logN)에 구할 수 있다.
⏳ 회고
귀여운 고양이