From 093fd94d4acc164a71549649a1e683cc3fbfaeea Mon Sep 17 00:00:00 2001 From: serot Date: Tue, 4 Mar 2025 13:28:42 +0900 Subject: [PATCH] =?UTF-8?q?solved=20=EB=B6=80=EB=8C=80=EB=B3=B5=EA=B7=80?= =?UTF-8?q?=20-=20144.01ms=20181MB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\265\234\354\247\200\354\210\230.java" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" diff --git "a/Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" "b/Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" new file mode 100644 index 0000000..cae7b25 --- /dev/null +++ "b/Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" @@ -0,0 +1,35 @@ +import java.util.*; + +class Solution { + public int[] solution(int n, int[][] roads, int[] sources, int destination) { + int[] answer = new int[sources.length]; + ArrayList[] map = new ArrayList[n+1]; + for (int i = 1; i < n+1; i++) { + map[i] = new ArrayList<>(); + } + for (int[] road : roads) { + map[road[0]].add(road[1]); + map[road[1]].add(road[0]); + } + + ArrayDeque que = new ArrayDeque<>(); + int[] visit = new int[n+1]; + Arrays.fill(visit, -1); + visit[destination] = 0; + que.add(destination); + while (!que.isEmpty()) { + int now = que.poll(); + for (int i : map[now]) { + if (visit[i] != -1) continue; + visit[i] = visit[now]+1; + que.add(i); + } + } + + for (int i = 0; i < sources.length; i++) { + answer[i] = visit[sources[i]]; + } + + return answer; + } +} \ No newline at end of file