Algorithm/Cases-Study
가장 먼 노드
Xi_kor
2021. 6. 29. 19:13
큐를 이용해서 풀었다. 일반적인 방법!
#include <string>
#include <vector>
#include <queue>
using namespace std;
vector <int> v[20100];
int ch[20100];
int solution(int n, vector<vector<int>> edge) {
int answer = 0;
for(int i=0; i<edge.size(); i++){
int x=edge[i][0];
int y=edge[i][1];
v[x].push_back(y);
v[y].push_back(x);
}
ch[1]=1;
queue <int> q;
q.push(1);
int maxi=-1;
while(!q.empty()){
int x=q.front();
q.pop();
if(ch[x]>maxi) maxi=ch[x];
for(int i=0; i<v[x].size(); i++){
if(ch[v[x][i]]==0){
q.push(v[x][i]);
ch[v[x][i]]=ch[x]+1;
}
}
}
for(int i=2; i<=n; i++){
if(ch[i]==maxi) answer++;
}
return answer;
}