https://school.programmers.co.kr/learn/courses/30/lessons/49189
vertex 의 원소 [n, m] : 노드 n과 m이 이어져 있다는 뜻import java.util.*;
class Solution {
public int solution(int n, int[][] edge) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for(int i = 1; i <= n; i++)
graph.put(i, new ArrayList<>());
for(int[] e : edge){
graph.get(e[0]).add(e[1]);
graph.get(e[1]).add(e[0]);
}
boolean[] visited = new boolean[n + 1];
Queue<int[]> q = new ArrayDeque<>();
q.add(new int[]{1, 0});
visited[1] = true;
int maxDist = 0, cnt = 0;
while(!q.isEmpty()){
int[] cur = q.remove();
if(maxDist < cur[1]){
maxDist = cur[1];
cnt = 1;
}
else if(maxDist == cur[1])
cnt++;
for(int next : graph.get(cur[0])){
if(visited[next]) continue;
else{
visited[next] = true;
q.add(new int[]{next, cur[1] + 1});
}
}
}
return cnt;
}
}