[20250314] BOJ / P5 / 트리와 경로 개수 쿼리 / 권혁준 #242
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/25638
🧭 풀이 시간
20분
👀 체감 난이도
✏️ 문제 설명
정점이 N개인 트리가 주어지고, 각 정점은 빨강 혹은 파랑으로 색칠되어 있다.$u$ 하나로 이루어지며, 다음을 구해야 한다.
쿼리를 Q개 처리해보자.
각 쿼리는 정수
🔍 풀이 방법
[사용한 알고리즘]
우선, 트리의 루트를 1로 잡고,$T_n$ 을 n을 루트로 하는 서브트리라고 가정한다.
$R_n$ 과 $B_n$ 을 각각 $T_n$ 에 존재하는 빨강, 파랑 정점의 수로 정의한다.
쿼리$u$ 에 대한 답이 될 수 있는 경로 중, 시작점과 끝점을 고르는 경우를 살펴보면 크게 세 가지 경우로 나눌 수 있다.
DFS 한 번으로 위 세 가지 값들을 모두 구해서 더한 값을 각각$answer_n$ 에 저장한다.
⏳ 회고
...