-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
83 lines (61 loc) · 2.03 KB
/
Main.java
File metadata and controls
83 lines (61 loc) · 2.03 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
import java.util.*;
import static java.util.Collections.sort;
public class Main {
static List<Integer> BFSresult = new ArrayList<>();
static List<Integer> DFSresult = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int V = sc.nextInt();
ArrayList<Integer>[] num = new ArrayList[N + 1];
for (int i = 0; i < N + 1; i++) {
num[i] = new ArrayList<Integer>();
}
for (int i = 0; i < M; i++) {
int S = sc.nextInt();
int E = sc.nextInt();
num[S].add(E);
num[E].add(S);
}
for (int i = 0; i < N + 1; i++) {
sort(num[i]);
}
int[] visited = new int[N + 1];
visited[V] = 1;
DFS(V, num,visited);
BFS(V, N, num);
for (int i = 0; i < DFSresult.size(); i++) {
System.out.printf("%d ", DFSresult.get(i));
}
System.out.printf("\n");
for (int i = 0; i < BFSresult.size(); i++) {
System.out.printf("%d ", BFSresult.get(i));
}
}
private static void DFS(int v, ArrayList<Integer>[] num, int[] visited) {
DFSresult.add(v);
for (int i = 0; i < num[v].size(); i++) {
if (visited[num[v].get(i)] == 0) {
visited[num[v].get(i)] = 1;
DFS(num[v].get(i),num,visited);
}
}
}
private static void BFS(int v, int n, ArrayList<Integer>[] num) {
Deque<Integer> dq = new LinkedList<>();
int[] visited = new int[n + 1];
visited[v] = 1;
dq.offer(v);
while (!(dq.isEmpty())) {
int now = dq.pollFirst();
BFSresult.add(now);
for (int i = 0; i < num[now].size(); i++) {
if (visited[num[now].get(i)] == 0) {
visited[num[now].get(i)] = 1;
dq.offer(num[now].get(i));
}
}
}
}
}