-
Notifications
You must be signed in to change notification settings - Fork 0
/
11724.js
39 lines (36 loc) · 933 Bytes
/
11724.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
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
function main(input) {
const [N, M] = input[0].split(" ").map(Number);
const connectMap = new Array(N + 1).fill(true).map((v) => []);
const visited = new Array(N + 1).fill(false);
let cnt = 0;
for (let i = 1; i <= M; i++) {
const [s, t] = input[i].split(" ").map(Number);
connectMap[s].push(t);
connectMap[t].push(s);
}
for (let i = 1; i <= N; i++) {
if (visited[i]) continue;
visited[i] = true;
cnt++;
const queue = [...connectMap[i]];
while (queue.length) {
const node = queue.pop();
if (visited[node]) continue;
visited[node] = true;
queue.push(...connectMap[node]);
}
}
console.log(cnt);
}