-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek2_Day1.java
More file actions
76 lines (69 loc) · 1.9 KB
/
Week2_Day1.java
File metadata and controls
76 lines (69 loc) · 1.9 KB
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
package Algorithm2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class Week2_Day1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
HashMap<Integer, HashSet<Integer>> Graph = new HashMap<>();
StringBuilder sb = new StringBuilder();
String[] info = br.readLine().split(" ");
int N = Integer.parseInt(info[0]);
int M = Integer.parseInt(info[1]);
int V = Integer.parseInt(info[2]);
for(int j=1; j<=N; j++) {
Graph.put(j, new HashSet<>());
} // 미리 셋팅
for(int i=0; i<M ; i++) {
String[] line = br.readLine().split(" ");
int N1 = Integer.parseInt(line[0]);
int N2 = Integer.parseInt(line[1]);
Graph.get(N1).add(N2);
Graph.get(N2).add(N1);
}
///////// DFS
Set<Integer> visit = new HashSet<>();
Stack<Integer> st = new Stack<>();
st.add(V);
while(!st.isEmpty()) {
int element = st.pop();
if(!visit.contains(element)) {
sb.append(element + " ");
visit.add(element);
Object[] graph_arr = Graph.get(element).stream().sorted(
(o1, o2) ->
Integer.compare(o2, o1)).toArray();
for(Object el : graph_arr) {
if(!visit.contains(el)) {
st.push((Integer) el);
}
}
}
}
System.out.println(sb.toString());
///////// BFS
sb = new StringBuilder();
visit = new HashSet<>();
Queue<Integer> qu = new ArrayDeque<>();
qu.add(V);
while(!qu.isEmpty()) {
int element = qu.poll();
if(!visit.contains(element)) {
sb.append(element + " ");
visit.add(element);
for(int el : Graph.get(element)) {
if(!visit.contains(el)) {
qu.offer(el);
}
}
}
}
System.out.println(sb.toString());
}
}