https://www.acmicpc.net/problem/10451
- DFS, BFS ๋ ๊ฐ์ง๋ก ํ ์ ์๋ ๋ฌธ์
- ์๊ฐ๋ณต์ก๋๋ O(N)์ผ๋ก DFS, BFS ๋์ผํ๋ค.
DFS (๊น์ด์ฐ์ ํ์) ํ์ด
- ๋ฌธ์ ๋ฅผ ๋ณด๋ฉด ์์ด์ 1๋ถํฐ ์ธ๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ ํฌ๊ธฐ ์ค์ ์ ์ฃผ์ํ๋ค.
- ์ ๋ ฅ๋ฐ์ ์์ด์ ๋ฐฐ์ด์ ๋ฃ์ ๋๋ ArrayIndexOutOfBoundsException๊ฐ ๋ฐ์ํ์ง ์๋๋ก ์ฃผ์ํ๋ค.
- ์์ด ๋ฐฐ์ด์ ์ธ๋ฑ์ค 1๋ถํฐ ๊ฐ์ ๋ฃ๊ธฐ ๋๋ฌธ์ ๊ทธ ๊ฐ์ ํด๋นํ๋ str ๋ฐฐ์ด์์๋ index-1์ ํด์ค์ผํ๋ค.
- ๋ ธ๋ ๋ฐฉ๋ฌธ์ ๊ธฐ๋กํ boolean ํ์ ์ visited ๋ฐฐ์ด์ ๋ง๋ค์ด์ ์ฌ์ดํด์ด ์ค๋ณต์ผ๋ก ์ฒดํฌ๋์ง ์๋๋ก ํ์ํ๋ค.
[ ํ์ ๊ณผ์ ]
* n = 1
dfs(1)
visited[1] = true
nextNode = 3
dfs(3)
visited[3] = true
nextNode = 7
dfs(7)
visited[7] = true
nextNode = 5
dfs(5)
visited[5] = true
nextNode = 1
-> visited[1] = true ์ด๋ฏ๋ก ํ์ ์ข ๋ฃํ๊ณ cycle์ ์นด์ดํธํ๋ค.
์ฒซ๋ฒ์งธ ํ์์ด ๋๋๋ฉด visited ๋ฐฐ์ด์ ์๋์ ๊ฐ์ ์ํ๊ฐ ๋ ๊ฒ์ด๋ค.
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
true | false | true | false | true | false | true | false |
- ์ด๋ฐ์์ผ๋ก dfs ์ฌ๊ทํจ์๋ฅผ ์คํํ๊ณ ํ์์ด ํ ๋ฒ ์๋ฃ๋๋ฉด ์์ด ์ฌ์ดํด์ ์นด์ดํธํ๋ค.
import java.io.*;
import java.util.*;
public class Main{
static boolean[] visited;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//ํ
์คํธ ์ผ์ด์ค ๊ฐ์ ์
๋ ฅ
int T = Integer.parseInt(br.readLine());
for(int i=0; i<T; i++){
//์์ดํฌ๊ธฐ ์
๋ ฅ
int N = Integer.parseInt(br.readLine());
arr = new int[N+1];
//๋
ธ๋ ๋ฐฉ๋ฌธ ๊ธฐ๋กํ ๋ฐฐ์ด
visited = new boolean[N+1];
//์์ด ์
๋ ฅ
String[] str = br.readLine().split(" ");
for(int j=1; j<=N; j++){
arr[j] = Integer.parseInt(str[j-1]);
}
int cycle = 0;
//ํ์
for(int n=1; n<=N; n++){
if(!visited[n]){ //๋ฐฉ๋ฌธํ์ง ์์ ๋
ธ๋์์ ์์
dfs(n);
cycle++;
}
}
System.out.println(cycle);
}
}
//DFS
static void dfs(int node){
//ํ์ฌ ๋
ธ๋ ๋ฐฉ๋ฌธ
visited[node] = true;
int nextNode = arr[node];
if(!visited[nextNode]){ //๋ค์ ๋
ธ๋ ๋ฐฉ๋ฌธ์ํ์ผ๋ฉด ํ์
dfs(nextNode);
}
}
}
BFS (๋๋น์ฐ์ ํ์) ํ์ด
- ์ ์ฒด์ ์ธ ์ฝ๋๋ DFS์ ๋๊ฐ๊ณ ํ์ํ๋ ๋ถ๋ถ๋ง ์ฐจ์ด๊ฐ ์๋ค.
- Queue๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๋ ๋ฒจ์ ๋ ธ๋๋ฅผ ์์ฐจ์ ์ผ๋ก ํ์ํ๋ค.
- ์ด ๋ฌธ์ ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ดค์ ๋ ๊ฐ์ธ์ ์ผ๋ก DFS ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข ๋ ๊ฐ๋จํ๋ค๊ณ ๋๊ผ๋ค.
- Queue์์ poll()์ ํ์ฌ๊ฐ์ ๋ฆฌํด, ๋น์ด์์ผ๋ฉด null์ ๋ฆฌํดํ๋ค.
import java.io.*;
import java.util.*;
public class Main{
static boolean[] visited;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//ํ
์คํธ ์ผ์ด์ค ๊ฐ์ ์
๋ ฅ
int T = Integer.parseInt(br.readLine());
for(int i=0; i<T; i++){
//์์ดํฌ๊ธฐ ์
๋ ฅ
int N = Integer.parseInt(br.readLine());
arr = new int[N+1];
//๋
ธ๋ ๋ฐฉ๋ฌธ ๊ธฐ๋กํ ๋ฐฐ์ด
visited = new boolean[N+1];
//์์ด ์
๋ ฅ
String[] str = br.readLine().split(" ");
for(int j=1; j<=N; j++){
arr[j] = Integer.parseInt(str[j-1]);
}
int cycle = 0;
//ํ์
for(int n=1; n<=N; n++){
if(!visited[n]){ //๋ฐฉ๋ฌธํ์ง ์์ ๋
ธ๋์์ ์์
bfs(n);
cycle++;
}
}
System.out.println(cycle);
}
}
//BFS
static void bfs(int start){
//Queue ์์ฑ
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
visited[start] = true;
while(!queue.isEmpty()){
int current = queue.poll(); //ํ์ฌ ๋
ธ๋
int nextNode = arr[current]; //์์ด์ ๋ฐ๋ฅธ ์ด๋
if(!visited[nextNode]){
visited[nextNode] = true;
queue.add(nextNode);
}
}
}
}
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Silver II/JAVA] 1260 DFS์ BFS (1) | 2024.12.10 |
---|---|
[Silver II/JAVA] 24479, 24480 ์๊ณ ๋ฆฌ์ฆ ์์ - ๊น์ด ์ฐ์ ํ์ 1, 2 (0) | 2024.12.01 |
[Silver V/JAVA] 13241 ์ต์๊ณต๋ฐฐ์ (0) | 2024.11.22 |
[Silver V/JAVA] 10815 ์ซ์์นด๋ (0) | 2024.11.18 |
[Bronze II/JAVA] 84753226 ์์ (0) | 2024.11.02 |