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/13208
🧭 풀이 시간
25분
👀 체감 난이도
✏️ 문제 설명
정점 N개, 간선 M개인 그래프가 있고, 각 정점에는 가중치가 존재한다.
이 그래프에 질문이 Q개 들어오면 전부 답해주자.
각 질문은 정수 S, E로 이루어져 있다.
질문의 답은 전화에 든 비용 중 최댓값이 된다.
이 값을 최소화해서 알려주자.
🔍 풀이 방법
이 문제는 N이 작아서, 두 사람이 밟고 있는 정점 상태의 가짓수가 N^2인데 이렇게 해도 250,000밖에 안 된다.
따라서 그래프를 N^2개의 정점이 있도록 리모델링한다. 이 때 간선의 개수는 약 4NM개 정도가 나오는데 M <= 3,000이라 2NM <= 3,000,000 이라 충분히 괜찮다.
이 때 새로 만든 그래프의 정점 가중치는 당연히 원래 그래프에서의 두 정점의 가중치 곱이 된다.
그럼 각 질문을 재해석할 수 있다.
새로 만든 그래프에서 (S, E) -> (E, S)로 가는 경로에 속한 정점의 가중치 중 최댓값이 질문의 답이 된다.
물론 이런 경로가 여러 개 존재할 수 있으므로, 최소 스패닝 트리를 구해서 최적 경로로만 이동하도록 할 수 있다.
그럼 이제 새로 만든 최소 스패닝 트리에서 질문의 답을 구하면 된다.
각 질문의 답을 로그 시간에 구해야 한다.
최소 스패닝 트리에서 아무 점이나 루트로 잡고, 희소 배열을 구축한다.
parent[n][k] = n번 정점에서 부모를 타고 2^k번 이동했을 때의 정점 번호
value[n][k] = n번 정점에서 부모를 타고 2^k번 이동했을 때의 정점 가중치 최댓값
-> parent[n][k] = parent[parent[n][k-1]][k-1]
-> value[n][k] = max(value[n][k-1], value[parent[n][k-1]][k-1])
이제 질문 (S, E)가 들어오면,
저 트리에서 정점 (S, E)와 (E, S)를 희소 배열로 위로 올리면서 value의 최댓값을 찾아 답해주면 된다.
⏳ 회고