๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ํ…€ ํ”„๋กœ์ ํŠธ BOJ #9466

by seolhee2750 2022. 8. 9.
๋ฌธ์ œ

https://www.acmicpc.net/problem/9466

 

9466๋ฒˆ: ํ…€ ํ”„๋กœ์ ํŠธ

์ด๋ฒˆ ๊ฐ€์„ํ•™๊ธฐ์— '๋ฌธ์ œ ํ•ด๊ฒฐ' ๊ฐ•์˜๋ฅผ ์‹ ์ฒญํ•œ ํ•™์ƒ๋“ค์€ ํ…€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ ํŒ€์› ์ˆ˜์—๋Š” ์ œํ•œ์ด ์—†๋‹ค. ์‹ฌ์ง€์–ด ๋ชจ๋“  ํ•™์ƒ๋“ค์ด ๋™์ผํ•œ ํŒ€์˜ ํŒ€์›์ธ ๊ฒฝ์šฐ์™€ ๊ฐ™์ด ํ•œ ํŒ€๋งŒ ์žˆ์„

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	static int[] stu;
	static boolean[] cycle, visited;
	static int cnt; // ์‹ธ์ดํด์— ํฌํ•จ๋œ ํ•™์ƒ ์ˆ˜
	
	public static void dfs(int num) {
		if(visited[num]) { // ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ (์‹ธ์ดํด !!!)
			cycle[num] = true;
			cnt++;
		} else visited[num] = true; // ์ฒซ ๋ฐฉ๋ฌธ์€ ๋ฐฉ๋ฌธ ์ฒดํฌ
		
		if(!cycle[stu[num]]) dfs(stu[num]); // ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ์‚ฌ์ดํด์— ํฌํ•จ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํƒ์ƒ‰
		cycle[num] = true; // ๋‹ค์Œ ํƒ์ƒ‰ ์‹œ ์ค‘๋ณต์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder out = new StringBuilder();
		
		int tc = Integer.parseInt(in.readLine());
		for(int t = 0; t < tc; t++) {
			int n = Integer.parseInt(in.readLine());
			stu = new int[n];
			visited = new boolean[n];
			cycle = new boolean[n];
			cnt = 0;
			
			st = new StringTokenizer(in.readLine());
			for(int i = 0; i < n; i++) stu[i] = Integer.parseInt(st.nextToken()) - 1;
			for(int i = 0; i < n; i++) if(!cycle[i]) dfs(i); // ์‹ธ์ดํด์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์ˆ˜๋ฉด ํƒ์ƒ‰
			
			out.append((n - cnt) + "\n"); // ์ „์ฒด ํ•™์ƒ ์ˆ˜ - ์‚ฌ์ดํด์— ํฌํ•จ๋œ ํ•™์ƒ ์ˆ˜
		}
		
		System.out.println(out);
	}
}

๐Ÿ‘‰ DFS ๋ฌธ์ œ

 

[ ๋ณ€์ˆ˜ ์„ค๋ช… ]

  • boolean[] visited
    ๋ฐฉ๋ฌธ ์ฒดํฌ (๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฌธ ์‹œ ์‹ธ์ดํด์ž„์„ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•จ)
  • boolean[] cycle
    ์‹ธ์ดํด ์ฒดํฌ (์‹ธ์ดํด์— ํฌํ•จ๋œ ํ•™์ƒ๋“ค์„ ์ฒดํฌํ•ด๋†“์Œ์œผ๋กœ์จ ์ค‘๋ณต ํƒ์ƒ‰์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ)
  • cnt
    ์‹ธ์ดํด์— ํฌํ•จ๋œ ํ•™์ƒ ์ˆ˜ ์นด์šดํŠธ

 

[ ๋ฌธ์ œ ํ’€์ด ]

  • ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๋Š” ์ˆ˜ num์ด visited์—์„œ true์ผ ๊ฒฝ์šฐ, ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๊ณ ,
    ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์„ ๋˜ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ์‹ธ์ดํด์ด ์ด๋ฃจ์–ด์กŒ๋‹ค๋Š” ์˜๋ฏธ !!
    => cycle์— true๋กœ ํ‘œ์‹œํ•ด์ฃผ๊ณ , cnt๋ฅผ ๋Š˜๋ ธ๋‹ค.
  • ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‹ค์Œ ๋…ธ๋“œ stu[num]์˜ cycle์ด false๋ผ๋ฉด
    ์•„์ง ์‹ธ์ดํด์„ ์ฐพ์ง€ ๋ชปํ•œ ๋…ธ๋“œ๋ผ๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ, ์ด์–ด์„œ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์žฌ๊ท€ํ˜ธ์ถœํ–ˆ๋‹ค.
  • ํ˜„์žฌ ๋…ธ๋“œ์˜ cycle์€ true๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด ์ถ”ํ›„ ๊ฐ™์€ ๊ณณ์„ ์ค‘๋ณตํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ–ˆ๋‹ค.
    • ์ฒ˜์Œ์— ์ด ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ๊ตฌํ˜„์ด ์–ด๋ ค์› ๋Š”๋ฐ, ์ถ”๊ฐ€๋กœ ์„ค๋ช…ํ•ด๋ณด์ž๋ฉด
      ๋ณดํ†ต์˜ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์—์„œ๋Š” ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ์œ„ํ•ด visited ๋ฐฐ์—ด ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.
    • ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ, ๋‘ ๋ฒˆ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ๊ฐ€ ์‹ธ์ดํด์„ ์ด๋ฃจ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ
      ํ•œ ๋ฒˆ ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์„ ์ฒดํฌํ•˜๋Š” ๊ฒƒ์„ ์ง„์งœ ๋ฐฉ๋ฌธ์ฒดํฌ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค.
      => visited ๋ฐฐ์—ด ํ•˜๋‚˜๋งŒ์œผ๋กœ๋Š” ๋ฐฉ๋ฌธ ์ฒดํฌ์— ํ•œ๊ณ„๊ฐ€ ์žˆ์Œ

 

โœ”๏ธ ๋ฐ˜๋ก€ ์ถ”์ฒœ
1
3
2 3 2

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์–ด๋ ต๋‹ค...... bfs๋Š” ๊ฝค ์ต์ˆ™ํ•˜๊ฒŒ ํ‘ผ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ! dfs๋Š” ์ง„์งœ ๋„ˆ๋ฌด ์–ด๋ ค์›Œ....
    ์ด๊ฑฐ ์žฌ๊ท€๋ฅผ ์ž˜ ๋ชปํ•ด์„œ ๊ทธ๋Ÿฐ๊ฑด๊ฐ€? ใ…œใ…œ ์žฌ๊ท€ ๋ฌธ์ œ๋„ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.
  • ์ด ๋ฌธ์ œ๋Š” ์˜ˆ์™ธ๋„ ๊ฝค ์žˆ๊ณ , ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ์ž˜ ๊ณ ๋ คํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฌธ์ œ.!!
    ์ด๋Ÿฐ ์‹์œผ๋กœ dfs๋Š” ์‹ธ์ดํด์„ ์ฐพ๋Š” ๋ฌธ์ œ๊ฐ€ ๋งŽ์€๋“ฏํ•œ๋ฐ ์—ฐ์Šต์ด ํ•„์š”ํ•˜๊ฒ ๋‹ค..

๋Œ“๊ธ€