[Programmers] 가장 먼 노드

Time Lapse :6min 0sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;

int solution(int n, vector<vector<int>> edge) {
vector<vector<int>> map(n+1,vector<int>(n+1,0));
bool visit[n+1];
memset(visit,false,sizeof(visit));
for(int i=0;i<edge.size();i++){
map[edge[i][0]][edge[i][1]]=1;
map[edge[i][1]][edge[i][0]]=1;
}
queue<int> q;
q.push(1);
visit[1]=true;
map[1][1]=0;
int dist = 0;
int answer = 0;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int i=1;i<=n;i++){
if((map[cur][i]==1)&&!visit[i]){
q.push(i);
map[i][i]=map[cur][cur]+1;
visit[i]=true;
if(dist==map[i][i]){
answer+=1;
}
else if(dist<map[i][i]){
answer = 1;
dist = map[i][i];
}
}
}

}
return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/farestNode/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.