728x90
BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean[][] connected;
static boolean[] checked;
public static void main(String[] args) throws IOException{
int N = Integer.parseInt(br.readLine());
int T = Integer.parseInt(br.readLine());
connected = new boolean[N+1][N+1];
checked = new boolean[N+1];
for(int i=0; i<T; i++){
StringTokenizer stk = new StringTokenizer(br.readLine()," ");
int a = Integer.parseInt(stk.nextToken());
int b = Integer.parseInt(stk.nextToken());
connected[a][b]=true;
connected[b][a]=true;
}
System.out.println(BFS(1));
}
static int BFS(int start) {
int res = 0;
Queue<Integer> q = new LinkedList<>();
q.offer(start);
while(!q.isEmpty()) {
int presNum = q.poll();
checked[presNum] = true;
for(int i=1; i<checked.length; i++) {
if(connected[presNum][i] && !checked[i]) {
q.offer(i);
checked[i]=true;
res++;
}
}
}
return res;
}
}
|
cs |
DFS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static boolean[][] connected; static boolean[] checked; static int cnt; public static void main(String[] args) throws IOException{ int N = Integer.parseInt(br.readLine()); int T = Integer.parseInt(br.readLine()); connected = new boolean[N+1][N+1]; checked = new boolean[N+1]; cnt=0; for(int i=0; i<T; i++){ StringTokenizer stk = new StringTokenizer(br.readLine()," "); int a = Integer.parseInt(stk.nextToken()); int b = Integer.parseInt(stk.nextToken()); connected[a][b]=true; connected[b][a]=true; } // System.out.println(BFS(1)); DFS(1); System.out.println(cnt-1); } static void DFS(int preNum) { checked[preNum]=true; cnt++; for(int i=1; i<checked.length; i++) { if(connected[preNum][i] && !checked[i]) DFS(i); } } static int BFS(int startNum) { int res = 0; Queue<Integer> q = new LinkedList<>(); q.offer(startNum); while(!q.isEmpty()) { int presNum = q.poll(); checked[presNum] = true; for(int i=1; i<checked.length; i++) { if(connected[presNum][i] && !checked[i]) { q.offer(i); checked[i]=true; res++; } } } return res; } } | cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 5430번 AC (0) | 2021.07.17 |
---|---|
[백준][Java] 10773번 제로 (0) | 2021.07.17 |
[백준][Java] 1181번 단어 정렬 (0) | 2021.06.27 |
[백준][Java] 2941번 크로아티아 알파벳 (0) | 2021.06.26 |
[백준][Java] 1316번 그룹 단어 체커 (0) | 2021.06.26 |