diff --git "a/lkhyun/202511/19 PGM Lv3 \352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.md" "b/lkhyun/202511/19 PGM Lv3 \352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.md" new file mode 100644 index 00000000..c8617985 --- /dev/null +++ "b/lkhyun/202511/19 PGM Lv3 \352\260\200\354\236\245 \353\250\274 \353\205\270\353\223\234.md" @@ -0,0 +1,48 @@ +```java +import java.util.*; +class Solution { + static int answer = 0; + static int max = 0; + static List[] adjList; + public int solution(int n, int[][] edge) { + answer = 0; + adjList = new List[n+1]; + for(int i = 0; i<=n; i++){ + adjList[i] = new ArrayList<>(); + } + + for(int[] e : edge){ + int a = e[0]; + int b = e[1]; + adjList[a].add(b); + adjList[b].add(a); + } + + BFS(n); + + return answer; + } + public static void BFS(int n){ + ArrayDeque q = new ArrayDeque<>(); + boolean[] visited = new boolean[n+1]; + q.offer(new int[]{1,0}); + visited[1] = true; + + while(!q.isEmpty()){ + int[] cur = q.poll(); + if(max < cur[1]){ + max = cur[1]; + answer = 1; + }else if(max == cur[1]){ + answer++; + } + for(int next : adjList[cur[0]]){ + if(!visited[next]){ + q.offer(new int[]{next,cur[1]+1}); + visited[next] = true; + } + } + } + } +} +```