-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp가장 먼 노드.js
42 lines (41 loc) · 897 Bytes
/
p가장 먼 노드.js
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
42
const solution = (n, edge) => {
let answer = 0;
const visited = new Array(n + 1).fill(0);
const graph = new Array(n + 1).fill(0).map((el) => []);
edge.forEach((el) => {
graph[el[0]].push(el[1]);
graph[el[1]].push(el[0]);
});
const bfs = () => {
const queue = [];
queue.push(1);
visited[1] = true;
while (queue.length !== 0) {
console.log(queue);
const qLength = queue.length;
answer = queue.length;
for (let i = 0; i < qLength; i++) {
let now = queue.shift();
for (let i = 0; i < graph[now].length; i++) {
if (!visited[graph[now][i]]) {
visited[graph[now][i]] = true;
queue.push(graph[now][i]);
}
}
}
}
};
bfs();
return answer;
};
console.log(
solution(6, [
[3, 6],
[4, 3],
[3, 2],
[1, 3],
[1, 2],
[2, 4],
[5, 2],
])
);