-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ11725.js
49 lines (37 loc) · 974 Bytes
/
BOJ11725.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
43
44
45
46
47
48
49
// Solution 1
function solution(n, nodes) {
const tree = Array.from({ length: n + 1 }, () => []);
const visited = Array.from({ length: n + 1 }, () => 0);
for (const [x, y] of nodes) {
tree[x].push(y);
tree[y].push(x);
}
function bfs() {
const queue = [];
visited[1] = 1;
for (const next of tree[1]) {
visited[next] = 1;
queue.push(next);
}
while (queue.length !== 0) {
const node = queue.shift();
for (const next of tree[node]) {
if (visited[next]) {
continue;
}
visited[next] = node;
queue.push(next);
}
}
}
bfs();
const results = visited.slice(2).join('\n');
return results;
}
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const n = parseInt(input[0]);
const nodes = input
.slice(1)
.map((row) => row.split(' ').map((value) => parseInt(value)));
console.log(solution(n, nodes));