Skip to content

Commit 61734b6

Browse files
committed
18352 특정 거리의 도시 찾기
1 parent ef54d68 commit 61734b6

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed
Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,79 @@
11
package bfs.silver.silver_2;
2-
import java.io.BufferedReader;
3-
import java.io.IOException;
4-
import java.io.InputStreamReader;
5-
import java.util.ArrayList;
6-
import java.util.LinkedList;
7-
import java.util.Queue;
8-
import java.util.StringTokenizer;
9-
public class _18352 {
10-
static Queue<Integer> q = new LinkedList<>();
11-
static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
122

13-
static int[] arr;
14-
static boolean check;
15-
static int N, M, K, X;
3+
import java.io.*;
4+
import java.util.*;
165

17-
public static void bfs(int startNode) {
18-
arr[startNode] = 0;
19-
q.offer(startNode);
6+
public class _18352 {
207

21-
while(!q.isEmpty()) {
22-
int n = q.poll();
8+
private static int n;
9+
private static int m;
10+
private static int k;
11+
private static int x;
12+
13+
private static List<List<Integer>> graph;
14+
private static List<Integer> nodes;
15+
private static boolean[] visited;
2316

24-
for(int node : graph.get(n)) {
25-
if(arr[node] == - 1) {
26-
arr[node] = arr[n] + 1;
27-
q.offer(node);
28-
}
29-
}
30-
}
31-
}
3217
public static void main(String[] args) throws IOException {
3318
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
3420
StringTokenizer st = new StringTokenizer(br.readLine());
21+
StringBuilder sb = new StringBuilder();
3522

36-
N = Integer.parseInt(st.nextToken()); // 도시 개수
37-
M = Integer.parseInt(st.nextToken()); // 도로 개수
38-
K = Integer.parseInt(st.nextToken()); // 거리 정보
39-
X = Integer.parseInt(st.nextToken()); // 출발 도시 번호
23+
n = Integer.parseInt(st.nextToken()); // 도시의 개수
24+
m = Integer.parseInt(st.nextToken()); // 도로의 개수
25+
k = Integer.parseInt(st.nextToken()); // 거리 정보
26+
x = Integer.parseInt(st.nextToken()); // 출발 도시의 정보
4027

41-
arr = new int[N + 1];
28+
graph = new ArrayList<>();
29+
nodes = new ArrayList<>();
30+
visited = new boolean[n + 1];
4231

43-
for(int i = 0; i < N + 1; i++) {
32+
for (int i = 0; i <= n; i++) {
4433
graph.add(new ArrayList<>());
45-
arr[i] = -1;
4634
}
47-
for(int i = 0; i < M; i++) {
35+
36+
for (int i = 0; i < m; i++) {
4837
st = new StringTokenizer(br.readLine());
4938

5039
int a = Integer.parseInt(st.nextToken());
5140
int b = Integer.parseInt(st.nextToken());
5241

5342
graph.get(a).add(b);
5443
}
55-
bfs(X);
5644

57-
check = false;
45+
bfs();
5846

59-
for(int i = 0; i <= N; i++) {
60-
if(arr[i] == K) {
61-
System.out.println(i);
62-
check = true;
63-
}
47+
Collections.sort(nodes);
48+
49+
for (int node : nodes) {
50+
sb.append(node).append("\n");
6451
}
65-
if(!check) {
66-
System.out.println(-1);
52+
53+
bw.write(sb.length() == 0 ? "-1" : sb.toString());
54+
bw.flush();
55+
bw.close();
56+
}
57+
58+
private static void bfs() {
59+
Queue<int[]> queue = new LinkedList<>();
60+
queue.add(new int[]{x, 0});
61+
visited[x] = true;
62+
63+
while (!queue.isEmpty()) {
64+
int[] cur = queue.poll();
65+
66+
if (cur[1] == k) {
67+
nodes.add(cur[0]);
68+
}
69+
70+
for (int next : graph.get(cur[0])) {
71+
if (!visited[next]) {
72+
visited[next] = true;
73+
queue.add(new int[]{next, cur[1] + 1});
74+
}
75+
}
6776
}
6877
}
78+
6979
}

0 commit comments

Comments
 (0)