-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_1260.java
More file actions
89 lines (67 loc) · 2.39 KB
/
baekjoon_1260.java
File metadata and controls
89 lines (67 loc) · 2.39 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
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
import java.io.*;
public class baekjoon_1260 {
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean[] visited;
static List<List<Integer>> edge;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int init = Integer.parseInt(st.nextToken());
visited = new boolean[N+1];
edge = new ArrayList<>();
for(int i=0;i<=N;i++){
edge.add(new ArrayList<>());
}
for(int i=0;i<M;i++){
st = new StringTokenizer(br.readLine());
int v1 = Integer.parseInt(st.nextToken());
int v2 = Integer.parseInt(st.nextToken());
edge.get(v1).add(v2);
edge.get(v2).add(v1);
}
for(List<Integer> list : edge){
Collections.sort(list);
}
dfs(init);
visited = new boolean[N+1];
bw.write("\n");
bfs(init);
bw.close();
}
public static void bfs(Integer init) throws IOException{
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(init);
while(!queue.isEmpty()){
Integer v = queue.poll();
visited[v] = true;
bw.write(String.format("%d ", v));
for(Integer i : edge.get(v)){
if(visited[i] == false){
queue.add(i);
visited[i] = true;
}
}
}
}
public static void dfs(Integer init) throws IOException {
Stack<Integer> st = new Stack<>();
st.push(init);
while (!st.isEmpty()) {
int v = st.pop();
if (!visited[v]) {
visited[v] = true;
bw.write(String.format("%d ", v));
List<Integer> thisEdge = edge.get(v);
for (int i = thisEdge.size() - 1; i >= 0; i--) {
int next = thisEdge.get(i);
if (!visited[next]) {
st.push(next);
}
}
}
}
}
}