[20250614] BOJ / P3 / 교수님은 기다리지 않는다 / 권혁준#353
Merged
ShinHeeEul merged 1 commit intomainfrom Jun 14, 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/3830
🧭 풀이 시간
55분
👀 체감 난이도
✏️ 문제 설명
상근이의 실험실에는 N개의 샘플이 있다.
샘플의 무게와 관련된 정보가 "! a b w"꼴로 주어지며, 이는 샘플 b가 샘플 a보다 w그램만큼 무겁다는 의미이다.
어떤 두 샘플의 무게를 비교하는 질문은 "? a b"꼴로 주어지며, 샘플 b가 샘플 a보다 몇 그램 더 무거운지 출력해야 한다.
두 종류의 작업은 합쳐서 총 M번 주어진다.
🔍 풀이 방법
[사용한 알고리즘]
무게가 전부 상대적으로만 주어지니까, 어떤 한 샘플을 기준으로 잡아야 한다.
기준 샘플의 무게를 0으로 두면, 배열 하나를 둬서 c[a] = 샘플 a의 무게로 정할 수 있다.
분리 집합을 이용해서 각 컴포넌트의 루트를 기준 샘플로 잡고, 정보가 주어질 때마다 union을 해줄 거다.
정보 "! a b w"가 주어지면, 우선 a와 b가 이미 비교 가능한 상태인지 확인한다.
비교 불가능하면, a와 b의 루트 x, y를 먼저 구한다.
새로 주어진 정보
b가 a보다 w그램 무거움과,기존에 알고 있는 정보
b는 y보다 c[b]그램 무거움,a는 x보다 c[a]그램 무거움를 조합하면y는 x보다 c[a]-c[b]+w그램 무거움라는 결론이 나온다.c[y] = c[a]-c[b]+w로 갱신하고 y를 x쪽에 붙인다.(분리 집합 union연산)
이 과정에서 y를 루트로 갖던 다른 점들에게 c[y]를 더해줘야 한다.
하지만 이걸 매번 더해주면 시간 초과가 난다.
따라서, find연산 시에만 더해줘서 꼭 필요한 정보만 미리 갱신하면 시간 내에 통과할 수 있다.
⏳ 회고
find연산에서 정보를 갱신할 때 로직이 잘못되어서 틀렸음
근데 뭐가 틀렸는지 찾는 데 오래걸림
코드를 빨리 짜는 것도 중요하지만 정확하게 짜는 게 더 중요함을 잊지 말자