๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 11724๋ฒˆ: ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜(dfs, bfs)

by ์ฃผ๋ฐœ2 2020. 2. 6.
๋ฐ˜์‘ํ˜•

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

 

11724๋ฒˆ: ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฐ„์„ ์˜ ์–‘ ๋์  u์™€ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ u, v ≤ N, u ≠ v) ๊ฐ™์€ ๊ฐ„์„ ์€ ํ•œ ๋ฒˆ๋งŒ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

dfs ์ฝ”๋“œ

import java.util.Scanner;

public class Main {
	static int N;	// ์ •์ 
	static int M;	// ๊ฐ„์„ 
	static int count;		// ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜
	static int graph[][];	// ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ 
	static boolean visit[];	// ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ฒดํฌ
	
	public static void dfs(int i) {
		visit[i] = true;
		for(int j=1; j<=N; j++) {
			if(graph[i][j] == 1 && visit[j] == false) {
				dfs(j);
			}
		}
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		M = scan.nextInt();
		graph = new int[N+1][N+1]; 
		visit = new boolean[N+1];
		
		for(int i=1; i<=M; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			graph[a][b] = graph[b][a] = 1;
		}
		
		for(int i=1; i<=N; i++) {
			if(!visit[i]) {
				dfs(i);
				count ++;
			}
		}
		
		System.out.println(count);
		scan.close();
	}

}

 

 

bfs ์ฝ”๋“œ

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	static int N;	// ์ •์ 
	static int M;	// ๊ฐ„์„ 
	static int count;		// ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜
	static int graph[][];	// ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ 
	static boolean visit[];	// ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ฒดํฌ
	
	public static void bfs(int i) {
		Queue<Integer> q = new LinkedList<Integer>();
		q.offer(i);
		visit[i] = true;
		
		while(!q.isEmpty()) {
			int temp = q.poll();
			for(int j=1; j<=N; j++) {
				if(graph[temp][j] == 1 && visit[j] == false) {
					q.offer(j);
					visit[j] = true;
				}
			}
		}
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		M = scan.nextInt();
		graph = new int[N+1][N+1]; 
		visit = new boolean[N+1];

		for(int i=1; i<=M; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			graph[a][b] = graph[b][a] = 1;
		}
		
		for(int i=1; i<=N; i++) {
			if(!visit[i]) {
				bfs(i);
				count ++;
			}
		}
		System.out.println(count);
		scan.close();
	}

}

 

 

ํ’€์ด

๊ธฐ์กด dfs ๋ฌธ์ œ์™€ ๋™์ผํ•˜๋‹ค.

์—ฐ๊ฒฐ ์š”์†Œ๋ž€ ๊ทธ๋ž˜ํ”„์—์„œ ์ •์ ๊ณผ ๊ฐ„์„ ์ด ๊ฒน์น˜์ง€ ์•Š๋Š” ๋ถ€๊ทธ๋ž˜ํ”„๋กœ, ๋ชจ๋“  ๋…ธ๋“œ์Œ์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋Š” ์กด์žฌํ•˜๋Š”๊ฑธ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

 

https://ratsgo.github.io/data%20structure&algorithm/2017/11/24/CC/

์œ„์™€ ๊ฐ™์€ ๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ์š”์†Œ๋Š” 4๊ฐœ๋‹ค.

๋”ฐ๋ผ์„œ ๋ชจ๋“  ์ •์ ์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ, ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ •์ ์ผ๊ฒฝ์šฐ dfs์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ๋Œ๊ณ , ์—ฐ๊ฒฐ ์š”์†Œ๋ฅผ ++ํ•ด์ค€๋‹ค.

 

 

 

์œ„ ์˜ˆ์ œ 1๋ฒˆ์˜ ๊ฒฝ์šฐ A4 ์ขŒ์ธก ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์—ฐ๊ฒฐ ์š”์†Œ๊ฐ€ 2๊ฐœ๊ฐ€ ๋˜๋Š”๋ฐ,, ์ง์ ‘ dfs๋ฌธ์„ ์ ์–ด๋ณด๋ฉด

 

 

 

dfs(1)

 -dfs(2)

  -dfs(5) ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ 

๋”์ด์ƒ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์ด ์—†์œผ๋ฏ€๋กœ ๋น ์ ธ๋‚˜์™€์„œ ๋‹ค์‹œ main ๋ฌธ์—์„œ i=3๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•œ๋‹ค.(count ++)

 

dfs(3)

 -dfs(4)

  -dfs(6) ์ด ํ˜ธ์ถœ๋˜๊ณ 

๋ฉ”์†Œ๋“œ๋Š” ์ข…๋ฃŒ๋œ๋‹ค. (count ++)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€