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

[๋ฐฑ์ค€] 2606๋ฒˆ: ๋ฐ”์ด๋Ÿฌ์Šค(DFS, BFS)

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

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

 

2606๋ฒˆ: ๋ฐ”์ด๋Ÿฌ์Šค

์ฒซ์งธ ์ค„์—๋Š” ์ปดํ“จํ„ฐ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋Š” 100 ์ดํ•˜์ด๊ณ  ๊ฐ ์ปดํ“จํ„ฐ์—๋Š” 1๋ฒˆ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ ๊ทธ ์ˆ˜๋งŒํผ ํ•œ ์ค„์— ํ•œ ์Œ์”ฉ ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ ์Œ์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

์ฝ”๋“œ 1(์ธ์ ‘ํ–‰๋ ฌ, DFS)

import java.util.Scanner;

public class Main {
	static int map[][];
	static boolean visit[];
	static int n, m, v;
	static int count = 0;
	
	public static int dfs(int i) {
		visit[i] = true;
		
		for(int j=1; j<=n; j++) {
			if(map[i][j] == 1 && visit[j] == false) {
				count ++;
				dfs(j);
			}
		}
		return count;
	}
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();	// ์ปดํ“จํ„ฐ ์ˆ˜(์ •์ )
		m = scan.nextInt();	// ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜(๊ฐ„์„ )
		v = 1;	// ํƒ์ƒ‰ ์‹œ์žฅํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ
		map = new int[n+1][n+1];	// ๊ฐ ์ •์ ๊ฐ„ ํƒ์ƒ‰ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
		visit = new boolean[n+1];	// ์ •์ ์˜ ํƒ์ƒ‰ ์—ฌ๋ถ€ ์ฒดํฌ
		
		for(int i=0; i<m; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			map[a][b] = map[b][a]= 1;
		}
		
		System.out.println(dfs(1));
		scan.close();
	}
}

 

์ฝ”๋“œ 2(์ธ์ ‘ํ–‰๋ ฌ, BFS)

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

public class Main {
	static int map[][];		// ๊ฐ ์ •์ ๊ฐ„ ํƒ์ƒ‰ ๊ฒฝ๋กœ ์ €์žฅ
	static boolean visit[];	// ์ •์  ํƒ์ƒ‰์—ฌ๋ถ€ ์ฒดํฌ
	static int n, m, v;		// ์ •์ , ๊ฐ„์„ , ์‹œ์ž‘ ์ •์ 
	static int count = 0;	// ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋ฐ”์ด๋Ÿฌ์Šค ๊ฑธ๋ฆฌ๋Š” ์ปดํ“จํ„ฐ ์ˆ˜
	
	public static int bfs(int i) {
		Queue<Integer> q = new LinkedList<Integer>();
		q.offer(i);
		visit[i] = true;
		while(!q.isEmpty()) {
			int temp = q.poll();
			
			for(int k=1; k<=n; k++) {
				if(map[temp][k] == 1 && visit[k] == false) {
					q.offer(k);
					visit[k] = true;
					count ++;
				}
			}
		}
		
		return count;
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();	// ์ปดํ“จํ„ฐ ์ˆ˜(์ •์ )
		m = scan.nextInt();	// ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜(๊ฐ„์„ )
		v = 1;	// ํƒ์ƒ‰ ์‹œ์žฅํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ
		map = new int[n+1][n+1];	// ๊ฐ ์ •์ ๊ฐ„ ํƒ์ƒ‰ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
		visit = new boolean[n+1];	// ์ •์ ์˜ ํƒ์ƒ‰ ์—ฌ๋ถ€ ์ฒดํฌ
		
		// ์ธ์ ‘ํ–‰๋ ฌ์„ ์ด์šฉํ•œ ํ’€์ด
		for(int i=0; i<m; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			map[a][b] = map[b][a]= 1;
		}
		
		System.out.println(bfs(1));
		scan.close();

	}

}

 

์ฝ”๋“œ 3(์ธ์ ‘๋ฆฌ์ŠคํŠธ, DFS)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	static ArrayList<Integer>[] a;
	static boolean visit[];	// ์ •์  ํƒ์ƒ‰์—ฌ๋ถ€ ์ฒดํฌ
	static int n, m, v;		// ์ •์ , ๊ฐ„์„ , ์‹œ์ž‘ ์ •์ 
	static int count = 0;	// ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋ฐ”์ด๋Ÿฌ์Šค ๊ฑธ๋ฆฌ๋Š” ์ปดํ“จํ„ฐ ์ˆ˜
	
	public static int dfs(int i) {
		visit[i] = true;
		for(int k : a[i]) {
			if(visit[k] == false) {
				count ++;
				dfs(k);
			}
		}
		return count;
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		n = scan.nextInt();	// ์ปดํ“จํ„ฐ ์ˆ˜(์ •์ )
		m = scan.nextInt();	// ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜(๊ฐ„์„ )
		v = 1;	// ํƒ์ƒ‰ ์‹œ์žฅํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ
		a = new ArrayList[n+1];	// ์ธ๋ฑ์Šค ํŽธ์˜์ƒ n+1์„ค์ •, 0๋ฒˆ์งธ ์š”์†Œ๋Š” ์‚ฌ์šฉ X
		visit = new boolean[n+1];
		for(int i=1; i<=n; i++) {
			a[i] = new ArrayList<Integer>();
		}
		
		for(int i=0; i<m; i++) {
			int u = scan.nextInt();	// ๊ฐ„์„ ์œผ๋กœ ์ด์–ด์ง„ ์ •์ 1
			int v = scan.nextInt();	// ์ •์ 1๊ณผ ๊ฐ„์„ ์œผ๋กœ ์ด์–ด์ง„ ์ •์ 2
			a[u].add(v);
			a[v].add(u);
		}
		
		System.out.println(dfs(v));
		
		scan.close();
	}

}

์ธ์ ‘๋ฆฌ์ŠคํŠธ DFS ์„ค๋ช… ์ž˜๋˜์žˆ๋Š”๊ณณ

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€