-
Notifications
You must be signed in to change notification settings - Fork 0
/
11724_linkedAmount.js
51 lines (45 loc) · 1.09 KB
/
11724_linkedAmount.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
50
51
// 연결 요소의 갯수 11724
let fs = require("fs");
let [meta, ...list] = fs.readFileSync("input.txt").toString().trim().split("\n");
let [nodes, links] = meta.split(" ").map(Number);
let wired = {};
let cnt = 0;
let visited = new Array(nodes + 1).fill().map((v) => 0);
for (let i = 0; i < list.length; i++) {
let data = list[i].split(" ").map(Number);
if (wired[data[0]]) {
wired[data[0]].push(data[1]);
} else {
wired[data[0]] = [data[1]];
}
if (wired[data[1]]) {
wired[data[1]].push(data[0]);
} else {
wired[data[1]] = [data[0]];
}
// wired[data[0]] = wired[data[0]] ? [...wired[data[0]], data[1]] : [data[1]];
// wired[data[1]] = wired[data[1]] ? [...wired[data[1]], data[0]] : [data[0]];
}
for (let i = 1; i <= nodes; i++) {
if (!visited[i]) {
dfs(i);
cnt++;
}
}
function dfs(node) {
if (visited[node]) return;
visited[node] = 1;
if (wired[node] === undefined) {
return;
}
for (let nxt of wired[node]) {
if (!visited[nxt]) {
dfs(nxt);
}
}
}
if (nodes > 1 || links > 0) {
console.log(cnt);
} else {
console.log(1);
}