사용 언어 : C++
BFS 로 구현
방문 노드 체크 할 때, 거리 넣어주는 방식
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int solution(int n, vector<vector<int>> edge) {
vector<int> graph[n+1];
//int visit[20001];
vector<int> visit(n+1, -1);
for(int i=0; i<edge.size(); i++){
graph[edge[i][0]].push_back(edge[i][1]);
graph[edge[i][1]].push_back(edge[i][0]);
visit.push_back(-1);
}
deque<int> q;
q.push_back(1);
visit[1] = 0;
while(!q.empty()){
int now = q.front();
q.pop_front();
for(int next : graph[now]){
if(visit[next] == -1){
visit[next] = visit[now] + 1;
q.push_back(next);
}
}
}
int answer = 0;
int max = *max_element(visit.begin(), visit.end());
cout << max << endl;
for(int i=1; i<n+1; i++){
if(visit[i] == max){
answer++;
}
}
return answer;
}