Skip to content

Commit d4e8a98

Browse files
committed
0926
1 parent a0bf411 commit d4e8a98

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BOJ/silver/BOJ1260/BOJ1260.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

BOJ/silver/BOJ1260/src/Main.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
import java.util.*;
3+
4+
import static java.util.Collections.sort;
5+
6+
public class Main {
7+
static List<Integer> BFSresult = new ArrayList<>();
8+
static List<Integer> DFSresult = new ArrayList<>();
9+
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int N = sc.nextInt();
13+
int M = sc.nextInt();
14+
int V = sc.nextInt();
15+
ArrayList<Integer>[] num = new ArrayList[N + 1];
16+
for (int i = 0; i < N + 1; i++) {
17+
num[i] = new ArrayList<Integer>();
18+
19+
}
20+
for (int i = 0; i < M; i++) {
21+
int S = sc.nextInt();
22+
int E = sc.nextInt();
23+
num[S].add(E);
24+
num[E].add(S);
25+
}
26+
for (int i = 0; i < N + 1; i++) {
27+
sort(num[i]);
28+
}
29+
int[] visited = new int[N + 1];
30+
visited[V] = 1;
31+
DFS(V, num,visited);
32+
BFS(V, N, num);
33+
34+
35+
for (int i = 0; i < DFSresult.size(); i++) {
36+
System.out.printf("%d ", DFSresult.get(i));
37+
}
38+
System.out.printf("\n");
39+
for (int i = 0; i < BFSresult.size(); i++) {
40+
System.out.printf("%d ", BFSresult.get(i));
41+
}
42+
}
43+
44+
private static void DFS(int v, ArrayList<Integer>[] num, int[] visited) {
45+
46+
DFSresult.add(v);
47+
48+
49+
for (int i = 0; i < num[v].size(); i++) {
50+
if (visited[num[v].get(i)] == 0) {
51+
visited[num[v].get(i)] = 1;
52+
DFS(num[v].get(i),num,visited);
53+
54+
}
55+
56+
}
57+
58+
}
59+
60+
61+
62+
63+
private static void BFS(int v, int n, ArrayList<Integer>[] num) {
64+
65+
Deque<Integer> dq = new LinkedList<>();
66+
int[] visited = new int[n + 1];
67+
visited[v] = 1;
68+
dq.offer(v);
69+
while (!(dq.isEmpty())) {
70+
int now = dq.pollFirst();
71+
BFSresult.add(now);
72+
for (int i = 0; i < num[now].size(); i++) {
73+
if (visited[num[now].get(i)] == 0) {
74+
visited[num[now].get(i)] = 1;
75+
dq.offer(num[now].get(i));
76+
}
77+
}
78+
79+
}
80+
81+
82+
}
83+
}

0 commit comments

Comments
 (0)