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/20531
🧭 풀이 시간
33분
👀 체감 난이도
✏️ 문제 설명
S인터넷고등학교에는
$N$ 명의 학생이 있다. 이들 사이에 몇몇은 서로 친구 관계를 맺고 있다. 친구 관계는 다음 세 가지 조건을 만족한다.
모든 학생은 자기 자신의 친구이다.$x$ 가 학생 $y$ 의 친구이면, 학생 $y$ 도 학생 $x$ 의 친구이다.$x$ 와 학생 $y$ 가 친구이고, 학생 $y$ 와 학생
$z$ 가 친구이면, 학생 $x$ 와 학생 $z$ 도 친구이다.
학생
학생
S인터넷고등학교의 김준원 선생님은 학생들의 친구 관계에 대해 알고 싶다. 다행히, 일부 학생 간의 친구 관계에 대한 정보는 이미 주어져있다. 김준원 선생님을 위해 전체 학생들의 친구 관계로 가능한 경우의 수를 구해보자.
🔍 풀이 방법
서로 친구 관계에 있는 두 사람은 하나의 집합에 넣어서 생각해도 똑같다는 관찰이 필요하다
=> 분리 집합으로 관리
그럼 문제는,$N$ 명이 존재할 때 가능한 경우의 수를 구하는 문제로 바뀐다.
dp[i][j] = i명이 존재할 때, 친구 관계를 적절히 맺어서 j개의 집합으로 줄이는 경우의 수라고 정의하면,
dp[i][j] = dp[i-1][j] * j + dp[i-1][j-1] 라는 식을 도출할 수 있다.
⏳ 회고
쿼리 형식의 문제인 줄 알았는데, 알고보니 DP 전처리가 핵심인 게 정말 참신한 문제인 것 같다