Skip to content

Commit 11f690b

Browse files
committed
[Silver III] Title: 바이러스, Time: 84 ms, Memory: 9448 KB -BaekjoonHub
1 parent d312be3 commit 11f690b

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

백준/Silver/2606. 바이러스/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 9436 KB, 시간: 92 ms
7+
메모리: 9448 KB, 시간: 84 ms
88

99
### 분류
1010

1111
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색
1212

1313
### 제출 일자
1414

15-
2026년 2월 11일 21:21:31
15+
2026년 2월 17일 16:47:53
1616

1717
### 문제 설명
1818

백준/Silver/2606. 바이러스/바이러스.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ let input = fs
77
.map((x) => x.replace("\r", ""));
88

99
const N = Number(input[0]);
10-
const M = Number(input[1]);
11-
12-
const visited = Array.from({ length: N + 1 }).fill(false);
13-
const graph = Array.from({ length: N + 1 }, () => []);
14-
15-
let cnt = 0;
16-
17-
for (let i = 2; i < M + 2; i++) {
10+
const T = Number(input[1]);
11+
let result = 0;
12+
const graph = Array.from({ length: N + 1 }, () => Array());
13+
for (let i = 2; i <= T + 1; i++) {
1814
const [x, y] = input[i].split(" ").map(Number);
1915
graph[x].push(y);
2016
graph[y].push(x);
2117
}
2218

23-
const queue = [];
24-
queue.push(1);
25-
visited[1] = true;
19+
const visited = Array(N + 1).fill(false);
20+
21+
dfs(1);
22+
23+
function dfs(V) {
24+
visited[V] = true;
2625

27-
while (queue.length) {
28-
const current = queue.shift();
29-
for (const next of graph[current]) {
26+
for (const next of graph[V]) {
3027
if (!visited[next]) {
31-
visited[next] = true;
32-
queue.push(next);
33-
cnt++;
28+
result++;
29+
dfs(next);
3430
}
3531
}
3632
}
3733

38-
console.log(cnt);
34+
console.log(result);

0 commit comments

Comments
 (0)