๋ฐ์ํ
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 ์ค๋ช ์๋์๋๊ณณ
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 9506๋ฒ: ์ฝ์๋ค์ ํฉ (0) | 2020.01.23 |
---|---|
[๋ฐฑ์ค] 11656๋ฒ: ์ ๋ฏธ์ฌ ๋ฐฐ์ด (0) | 2020.01.22 |
[๋ฐฑ์ค] 1260๋ฒ: DFS์ BFS (0) | 2020.01.22 |
ํ๋ก๊ทธ๋๋จธ์ค[Java] - ๋ ๋ฐ๋จน๊ธฐ (2) | 2020.01.22 |
[๋ฐฑ์ค] 10815๋ฒ: ์ซ์ ์นด๋ (1) | 2020.01.22 |
๋๊ธ