[๋ฌธ์ ๋งํฌ]
https://www.acmicpc.net/problem/1260
[ํ์ด ์์ค]
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class DfsBfs1260 {
public static int nodeCount;
public static LinkedList<Integer>[] nearNodeList;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
nodeCount=Integer.parseInt(st.nextToken());
int EdgeCount=Integer.parseInt(st.nextToken());
int startNode=Integer.parseInt(st.nextToken());
// ์ธ์ ๋
ธ๋๋ฐฐ์ด ์ ์ธ
nearNodeList=new LinkedList[nodeCount+1];
for(int i=0; i<=nodeCount;i++) {
nearNodeList[i]=new LinkedList<Integer>();
}
// ์
๋ ฅ๋ฐ์ ์ธ์ ๋
ธ๋๋ฐฐ์ด๊ตฌ์ฑ
while(EdgeCount-->0) {
st= new StringTokenizer(br.readLine());
int node1=Integer.parseInt(st.nextToken());
int node2=Integer.parseInt(st.nextToken());
// ์ธ์ ๋
ธ๋๋ฐฐ์ด์ ์ถ๊ฐํ์ฌ ์ ๋ฆฌ
nearNodeList[node1].add(node2);
nearNodeList[node2].add(node1);
Collections.sort(nearNodeList[node1]);
Collections.sort(nearNodeList[node2]);
}
// ๋ต์ ์ํ sb์ ์ธ
StringBuilder dfsWay=new StringBuilder();
StringBuilder bfsWay=new StringBuilder();
// ๋ฐฉ๋ฌธ์ฌ๋ถ๋ฅผ ์ฒดํฌํ๋ boolean๋ฐฐ์ด
boolean[] dfsVisited=new boolean[nodeCount+1];
boolean[] bfsVisited=new boolean[nodeCount+1];
dfs(startNode, dfsVisited, dfsWay);
bfs(startNode, bfsVisited, bfsWay);
System.out.println(dfsWay+"\n"+bfsWay);
}
// DFS : Stack์ด์ฉ or "์ฌ๊ทํจ์"
public static void dfs(int startNode, boolean[] visited, StringBuilder way) {
// ์์๋
ธ๋(ํด๋น๋
ธ๋)์ ๋ฐฉ๋ฌธ์ฌ๋ถ ์ฒดํฌ >> ๋ฐฉ๋ฌธ์ ์ข
๋ฃ
if(visited[startNode]) return;
// ๋ฏธ๋ฐฉ๋ฌธ์ >> ๋ฐฉ๋ฌธ ์ฒดํฌ ํ ์์๋ก ๊ธฐ๋ก
visited[startNode]=true;
way.append(startNode+" ");
// ์์๋
ธ๋ ํ์(๋ฐ๋ณต) >> ์ฌ๊ทํจ์
for(int nextNode:nearNodeList[startNode]) {
dfs(nextNode, visited, way);
}
}
// BFS : Queue์ด์ฉ
public static void bfs(int startNode, boolean[] visited, StringBuilder way) {
Queue<Integer> queue=new LinkedList<Integer>();
// ์์๋
ธ๋ ํ์ ์ฝ์
queue.offer(startNode);
while(!queue.isEmpty()) {
// ํ์ํ ๋
ธ๋๋ฅผ pop!
startNode=queue.poll();
// ํ์๋
ธ๋์ ๋ฐฉ๋ฌธ์ฌ๋ถ ํ์ธ
// => ๋ฐฉ๋ฌธ : ๋์ด๊ฐ๊ธฐ
if(visited[startNode]) continue;
// => ๋ฏธ๋ฐฉ๋ฌธ : ๋ฐฉ๋ฌธ ์ฒดํฌ ํ ๋ฐฉ๋ฌธ๊ธฐ๋ก
visited[startNode]= true;
way.append(startNode+" ");
// : ํด๋น ์์์ ํ์ ๋ชจ๋ ์ฝ์
for (int nextNode:nearNodeList[startNode]) {
queue.offer(nextNode);
}
}
}
}
|
cs |
[๋๋์ ]
- DFS : ์ฌ๊ทํจ์ or Stack ์ฌ์ฉ
- BFS : Queue ์ฌ์ฉ, ์ฌ๊ท๋์์ฌ์ฉX!!
'Algoritm > Quiz-Solutions' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode 1] two Sum (0) | 2020.08.02 |
---|---|
[๋ฐฑ์ค 2156] ํฌ๋์ฃผ ์์ (Java) - DP (0) | 2020.05.07 |
[๋ฐฑ์ค 9012] ๊ดํธ(Java) - Stack (0) | 2020.05.04 |
[๋ฐฑ์ค 10854] ํ (JAVA) - Queue (0) | 2020.05.03 |
[๋ฐฑ์ค 15953] ์๊ธํํฐ (JAVA) (0) | 2020.05.02 |
๋๊ธ