diff --git "a/suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" "b/suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" new file mode 100644 index 00000000..d2ba27ea --- /dev/null +++ "b/suyeun84/202509/01 PGM LV3 \353\266\200\353\214\200\353\263\265\352\267\200.md" @@ -0,0 +1,45 @@ +```java +import java.util.*; +class Solution { + int[] answer; + int[] result; + List graph[]; + + public int[] solution(int n, int[][] roads, int[] sources, int destination) { + answer = new int[n+1]; + result = new int[sources.length]; + graph = new ArrayList[n+1]; + Arrays.fill(answer, -1); + + for (int i = 0; i < n+1; i++) { + graph[i] = new ArrayList<>(); + } + + for (int[] road : roads) { + graph[road[0]].add(road[1]); + graph[road[1]].add(road[0]); + } + + bfs(destination); + + for (int i = 0; i < sources.length; i++) { + result[i] = answer[sources[i]]; + } + return result; + } + public void bfs(int start) { + Queue q = new ArrayDeque<>(); + q.add(start); + answer[start] = 0; + while (!q.isEmpty()) { + int curr = q.poll(); + for (int next : graph[curr]) { + if (answer[next] == -1) { + answer[next] = answer[curr] + 1; + q.add(next); + } + } + } + } +} +```