[20250831] BOJ / D5 / 그래프와 쿼리 / 권혁준 #785
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/16911
🧭 풀이 시간
90분
👀 체감 난이도
✏️ 문제 설명
간선이 없이 N개의 정점으로 이루어진 그래프에 쿼리들을 수행해보자.
🔍 풀이 방법
각 쿼리가 들어온 시점을 모두 기록해두고, 시간을 기준으로 분할 정복을 하게 되면, 시간 구간이 총 NlogN개 만들어진다.
특정 간선의 lifetime을 미리 계산해두고 각각의 시간 구간에 완전히 포함될 때만 그 간선을 해당 시간 구간에 저장해둔다.
마지막에 dfs로 시간 구간 트리를 순회하면서 각 구간에 속하는 간선들을 분리 집합으로 이었다가 마지막에 다시 rollback해준다. 이렇게 하면 연결성 판별이 가능.
rollback을 하기 위해서, 연산 최적화 시 경로 압축 대신 union by rank를 이용해 find연산을 O(logN)으로 줄인다.
⏳ 회고
배워가는 게 정말 많았음.
분리 집합 union연산이 rollback 가능하다는 것을 알게 됐고,
시간 축을 기준으로 분할 정복을 수행하는 관찰도 도움이 많이 되었다.