๐ก ๋ฌธ์ ์์ฝ
- ์ง์ค์ ์๋ ์ฌ๋๋ค์ด ์ผ๋ถ ์๊ณ , M๊ฐ์ ํํฐ๊ฐ ์ด๋ฆผ
- ํํฐ์ ์ฐธ์ฌํ ์ฌ๋ ์ค ์ง์ค์ ์๋ ์ฌ๋์ด ์๋ค๋ฉด, ํด๋น ํํฐ์์ ๊ฑฐ์ง๋ง์ ํ ์ ์์
- ํ ๋ฒ ์ง์ค์ ์๊ฒ ๋๋ฉด ์ฐ๊ฒฐ๋ ํํฐ, ์ฌ๋์ ํตํด ์ ํ๋จ (BFS ํ์ฉ)
- ๊ฑฐ์ง๋ง์ ํ ์ ์๋ ํํฐ์ ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์
๐ ์กฐ๊ฑด ์์ฝ
- ์ฌ๋ ์ N (1 ≤ N ≤ 50)
- ํํฐ ์ M (1 ≤ M ≤ 50)
- ์ง์ค์ ์๋ ์ฌ๋ ์ K (0 ≤ K ≤ N)
- ๊ฐ ํํฐ์ ์ฐธ์ํ ์ฌ๋๋ค์ ๋ชฉ๋ก ์ฃผ์ด์ง
๐ ์ ๋ ฅ ํ์
N M
K ์ง์ค์ ์๋ ์ฌ๋ ๋ฒํธ๋ค...
ํํฐ1 ์ฐธ์ ์ธ์ ์ ๋ฒํธ ๋ฒํธ ...
ํํฐ2 ์ฐธ์ ์ธ์ ์ ๋ฒํธ ๋ฒํธ ...
...
โ ์ถ๋ ฅ ํ์
- ๊ฑฐ์ง๋ง์ ํ ์ ์๋ ํํฐ์ ์ ์ถ๋ ฅ
๐ง ์๊ณ ๋ฆฌ์ฆ ์ค๊ณ
- ์ง์ค์ ์๋ ์ฌ๋๋ค์ ํ์ ์ ์ฅ (BFS ์์์ )
- ๊ฐ ์ฌ๋์ด ์ด๋ค ํํฐ์ ์ฐธ์ํ๋์ง ๊ธฐ๋ก
- BFS๋ก ์ง์ค ์ ํ:
- ์ง์ค์ ์๋ ์ฌ๋์ด ์ฐธ์ํ ํํฐ๋ผ๋ฉด ํด๋น ํํฐ์ ๋ค๋ฅธ ์ฐธ์์๋ ์ง์ค์ ์๊ฒ ๋จ
- ์ ํ๋ ์ฌ๋๋ค๋ ๋ค์ ํ์ ๋ฃ์ด ํ์ ๋ฐ๋ณต
- BFS ์ข ๋ฃ ํ, ์ง์ค์ด ์ ํ๋์ง ์์ ํํฐ ์ ์ธ๊ธฐ
๐ ํต์ฌ ๊ตฌํ ์ฝ๋
Queue<Integer> queue = new ArrayDeque<>();
for (์ง์ค์ ์๋ ์ฌ๋ p)
trueList[p] = 1; queue.offer(p);
while (!queue.isEmpty()) {
int person = queue.poll();
for (int partyIdx : peopleAttendedParty.get(person)) {
if (partyVisited[partyIdx]) continue;
partyVisited[partyIdx] = true;
for (int p : parties.get(partyIdx)) {
if (trueList[p] == 0) {
trueList[p] = 1;
queue.offer(p);
}
}
}
}
๐ ์ ์ฒด ์ฝ๋
import java.io.*;
import java.util.*;
public class Main {
public static Integer convertInt(String word) {
return Integer.parseInt(word);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = convertInt(st.nextToken());
int M = convertInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int trueNum = convertInt(st.nextToken());
int[] trueList = new int[N + 1];
Queue<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < trueNum; i++) {
int p = convertInt(st.nextToken());
trueList[p] = 1;
queue.offer(p);
}
List<List<Integer>> parties = new ArrayList<>();
List<List<Integer>> peopleAttendedParty = new ArrayList<>();
for (int i = 0; i <= N; i++) {
peopleAttendedParty.add(new ArrayList<>());
}
for (int i = 0; i < M; i++) {
parties.add(new ArrayList<>());
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int partyNum = convertInt(st.nextToken());
for (int j = 0; j < partyNum; j++) {
int p = convertInt(st.nextToken());
parties.get(i).add(p);
peopleAttendedParty.get(p).add(i);
}
}
boolean[] partyVisited = new boolean[M];
while (!queue.isEmpty()) {
int person = queue.poll();
for (int partyIdx : peopleAttendedParty.get(person)) {
if (partyVisited[partyIdx]) continue;
partyVisited[partyIdx] = true;
for (int p : parties.get(partyIdx)) {
if (trueList[p] == 0) {
trueList[p] = 1;
queue.offer(p);
}
}
}
}
int count = 0;
for (int i = 0; i < M; i++) {
if (!partyVisited[i]) {
count++;
}
}
System.out.println(count);
}
}
โฑ๏ธ ์๊ฐ ๋ณต์ก๋
๋จ๊ณ ์๊ฐ
| ์ ๋ ฅ ์ฒ๋ฆฌ | O(N + M) |
| BFS ํ์ | O(N + M) |
| ์ถ๋ ฅ ๊ณ์ฐ | O(M) |
โฎ๏ธ ์ ์ฒด์ ์ผ๋ก O(N + M) ์์ค์ ์๊ฐ ๋ณต์ก๋
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ฐฑ์ค 1976: ์ฌํ ๊ณํ(Java) (0) | 2025.05.24 |
|---|---|
| ํ๋ก๊ทธ๋๋จธ์ค PCCP๋ชจ์๊ณ ์ฌ#1 4๋ฒ๋ฌธ์ (0) | 2025.05.14 |
| ๋ฐฑ์ค 2075: N๋ฒ์งธ ํฐ ์(JAVA) (0) | 2025.05.06 |
| ๋ฐฑ์ค 13114: List of Unique Numbers(ํ์ด์ฌ) (0) | 2025.04.28 |
| 22251๋ฒ: ๋น๋ฐ ํธ์(ํ์ด์ฌ) (0) | 2025.04.26 |