# 68. 核酸检测
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let lineCount = 0;
let N = 0;
let confirmdCases = [];
let contacts = [];
let visited = [];
rl.on('line', function(line) {
if (lineCount === 0) {
N = parseInt(line);
visited = new Array(N).fill(false);
} else if(lineCount === 1) {
confirmdCases = line.split(',').map(Number);
contacts = new Array(N).fill(null).map(() => new Array(N).fill(false));
} else {
let row = line.split(',').map(Number);
contacts[lineCount - 2] = row.map(v => v===1);
if (lineCount - 2 === N - 1) {
rl.close();
}
}
lineCount++;
});
rl.on('close', () => {
confirmdCases.forEach(i => dfs(contacts, visited, i));
let count = 0;
visited.forEach((hasVisited, i) => {
if (hasVisited && !confirmdCases.includes(i)) {
count++;
}
})
console.log(count);
})
function dfs(contacts, visited, start) {
visited[start] = true;
for(let i=0; i<contacts.length; i++) {
if (contacts[start][i] && !visited[i]) {
dfs(contacts, visited, i);
}
}
}
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
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
← 67. 生成哈夫曼树 69. 贪吃的猴子 →