-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.java
More file actions
95 lines (78 loc) · 2.47 KB
/
1260.java
File metadata and controls
95 lines (78 loc) · 2.47 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
90
91
92
93
94
95
import java.io.*;
import java.util.*;
public class Main {
public static int N;
public static int M;
public static int V;
public static ArrayList<Integer>[] adj;
public static boolean[] visited;
public static ArrayList<Integer> result;
public static Queue<Integer> q;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
V = Integer.parseInt(st.nextToken());
//dfs
visited = new boolean[N+1];
adj = new ArrayList[N+1];
result = new ArrayList<>();
for(int k=1;k<N+1;k++){
adj[k] = new ArrayList<>();
}
for(int i = 0;i<M;i++){
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
adj[start].add(end);
adj[end].add(start);
}
// 작은 정점 번호 부터 방문하기 위해
for(int j = 0;j<N+1;j++){
if(adj[j]!=null)
Collections.sort(adj[j]);
}
//dfs
result.add(V);
dfs(V);
for(int i:result){
System.out.printf("%d ",i);
}
//bfs
System.out.println();
for(int j:bfs(V)){
System.out.printf("%d ", j);
}
}
public static void dfs(int here){
visited[here] = true;
for(int i = 0;i<adj[here].size();i++){
int there = adj[here].get(i);
if(!visited[there]){
result.add(there);
dfs(there);
}
}
}
public static ArrayList<Integer> bfs(int start){
q = new LinkedList<>();
ArrayList<Integer> bfsResult= new ArrayList<>();
boolean[] visit = new boolean[N+1];
q.add(start);
visited[start] = true;
while(!q.isEmpty()){
int here = q.poll();
visit[here] = true;
bfsResult.add(here);
for(int i =0;i<adj[here].size();i++){
int there = adj[here].get(i);
if(!visit[there]){
q.add(adj[here].get(i));
visit[there] = true;
}
}
}
return bfsResult;
}
}