[20260312] BOJ / D4 / Internet Monopoly / 권혁준#2016
Merged
ShinHeeEul merged 1 commit intomainfrom Mar 11, 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/29851
🧭 풀이 시간
45분
👀 체감 난이도
✏️ 문제 설명
정점 N개인 간선 없는 그래프가 주어진다. 두 종류의 쿼리를 총 Q개 처리해보자.
+ a b: 그래프에 간선 (a, b)를 추가한다.? k: 현재 그래프에서 k개의 간선들은 가중치를 1로 정하고 나머지 간선들의 가중치는 2로 정할 때, 가중치를 잘 분배해서 이 그래프의 최소 스패닝 트리가 유일하도록 만들 수 있을까?🔍 풀이 방법
일단 아무 MST를 하나 구성하고, MST에 쓰이지 않은 간선들 모두가 MST의 대체 간선이 될 수 없다면 그 MST는 유일하다고 봐도 된다.
대체 간선이라는 것은, 그 간선을 넣고 MST에 존재하는 다른 간선을 하나 빼도 MST를 유지하는 것을 의미한다.
따라서, MST에 쓰이지 않은 간선 (u, v)에 대해,
MST 상에서 u와 v를 잇는 경로 상의 간선들 중 (u, v)와 가중치가 같은 간선이 있다면 (u, v)는 대체 간선이 된다.
반대로 생각하면, MST가 유일하려면 MST에 쓰이지 않은 간선들은 모두 가중치가 2여야 하고,
그 간선들의 양 끝 점을 잇는 MST 상에서의 경로에 존재하는 간선 가중치는 모두 1이 되어야 한다.
이렇게 해서 특정 간선들의 가중치는 1 혹은 2로 고정을 시키고, 가중치가 고정되지 않은 나머지 간선들은 가중치가 뭐든 상관 없다.
따라서, 가중치가 1로 고정된 간선의 개수를 d라고 하면, d <= k <= N-1인 경우에만 MST가 유일하다.
구현을 효율적으로 하려면 쿼리 응답하기 전에 미리 트리를 알아야 해서, 일단 크루스칼 알고리즘으로 MST부터 구성해줬다.
다음으로, 쿼리를 다시 훑으며 MST에 사용되지 않은 간선이 등장한 경우에는 가중치를 1로 고정시키는 작업을 유니온 파인드로 수행했다.
⏳ 회고
재밌다