[20260310] BOJ / D5 / ACM Tax / 권혁준#2009
Merged
ShinHeeEul merged 1 commit intomainfrom Mar 9, 2026
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/13892
🧭 풀이 시간
80분
👀 체감 난이도
✏️ 문제 설명
정점 N개인 트리가 주어지며, 간선에는 가중치가 달려있다.
아래 쿼리를 Q개 처리해보자.
🔍 풀이 방법
경로 상의 간선 가중치를 naive하게 훑으면 O(NQ)라서 터진다.
중앙값을 찾는 작업을 변형해서 생각해봤다.
a, b, k에 대해
kthValueOnPath함수를 a와 b를 잇는 경로에서 k번째로 작은 간선의 가중치를 반환한다고 정의를 내렸다.그러면, a와 b를 잇는 경로의 길이 dist에 대해
kthValueOnPath(a, b, dist/2)를 구해 문제를 해결할 수 있다.다음은,
kthValueOnPath를 어떻게 구현해야 시간 내에 들어올 수 있을지 고민해봤다.a와 b를 잇는 경로에서 v이하의 가중치 개수를 효율적으로 구할 수 있다면, v를 이분 탐색으로 찾아서 빠르게 구할 수 있다.
countOnPathLeqThanK함수를 a와 b를 잇는 경로에서 k 이하의 가중치 개수를 반환한다고 정의를 내렸다.그리고,
countOnPathLeqThanK의 구현에 대해 생각해봤다.만약, 트리가 아니라 수열 형태의 선형 구조였다면 머지 소트 트리를 구성하여 해결할 수 있다.
따라서, Heavy-Light 분할을 이용해 트리를 체인 형태로 편 뒤 머지 소트 트리를 구성했다.
⏳ 회고
구현할 게 많아서 너무 힘들었다. 태그랑 다르게 푼 것 같은데, 나중에 정해를 찾아봐야 할 듯