[20251107] BOJ / P4 / 트리와 XOR / 권혁준 #1338
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/30239
🧭 풀이 시간
20분
👀 체감 난이도
✏️ 문제 설명
정점 N개인 트리에서 i번 정점에는 정수 a[i]가 쓰여 있다.
시행 한 번에서 정점 v와 정수 c를 선택하고, v를 루트로 하는 서브트리의 모든 정점에 쓰인 수 a[i]를 a[i] xor c로 바꾼다. 이때 시행의 비용은 (v를 루트로 하는 서브트리의 크기)*c이다.
r번 점이 트리의 루트인 경우에 모든 정점에 적힌 수를 같게 하기 위한 최소 비용을 m[r]이라고 할 때, m[1], m[2], ..., m[N]을 구해보자.
🔍 풀이 방법
일단 1번 점을 루트로 잡는다.
d[n] = n번 점을 루트로 하는 서브트리의 모든 점을 a[n]으로 만들기 위한 최소 비용u[n] = n번 점을 루트로 하는 서브트리 외의 모든 점을 a[n]으로 만들기 위한 최소 비용n의 자식들을 c라 하면,
d[n] = sum(d[c] + (a[n]^a[c]) * s[c])
u[n] = u[p] + d[p] - d[n] + (a[n]^a[p]) * (N - 2*s[n])
m[n] = d[n] + u[n]
⏳ 회고
ez