Skip to content

Commit fe43bcd

Browse files
committed
[Silver III] Title: 바이러스, Time: 92 ms, Memory: 9436 KB -BaekjoonHub
1 parent ddb7099 commit fe43bcd

2 files changed

Lines changed: 31 additions & 28 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-
메모리: 9428 KB, 시간: 120 ms
7+
메모리: 9436 KB, 시간: 92 ms
88

99
### 분류
1010

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

1313
### 제출 일자
1414

15-
2023년 10월 17일 16:37:03
15+
2026년 2월 11일 21:21:31
1616

1717
### 문제 설명
1818

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
/* /dev/stdin */
2-
let fs = require('fs');
3-
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
1+
// /dev/stdin
2+
const fs = require("fs");
3+
let input = fs
4+
.readFileSync("/dev/stdin")
5+
.toString()
6+
.split("\n")
7+
.map((x) => x.replace("\r", ""));
48

5-
const n = Number(input[0]);
6-
const m = Number(input[1]);
7-
// let graph = Array(n+1).fill([]);
8-
let graph = [];
9-
for(let i = 1; i <= n; i++) graph[i] = [];
10-
let visited = Array(n+1).fill(false);
11-
let answer = 0;
9+
const N = Number(input[0]);
10+
const M = Number(input[1]);
1211

12+
const visited = Array.from({ length: N + 1 }).fill(false);
13+
const graph = Array.from({ length: N + 1 }, () => []);
1314

14-
for(let i = 2; i <= m + 1; i++){
15-
let [a, b] = input[i].split(' ').map(Number);
16-
graph[a].push(b);
17-
graph[b].push(a);
18-
}
19-
20-
// console.log(graph)
15+
let cnt = 0;
2116

22-
dfs(graph, 1, visited)
17+
for (let i = 2; i < M + 2; i++) {
18+
const [x, y] = input[i].split(" ").map(Number);
19+
graph[x].push(y);
20+
graph[y].push(x);
21+
}
2322

24-
console.log(answer - 1)
23+
const queue = [];
24+
queue.push(1);
25+
visited[1] = true;
2526

26-
function dfs(graph, v, visited) {
27-
visited[v] = true;
28-
answer++
29-
30-
for(i of graph[v]){
31-
if(!visited[i]) {
32-
dfs(graph, i, visited);
27+
while (queue.length) {
28+
const current = queue.shift();
29+
for (const next of graph[current]) {
30+
if (!visited[next]) {
31+
visited[next] = true;
32+
queue.push(next);
33+
cnt++;
3334
}
3435
}
3536
}
37+
38+
console.log(cnt);

0 commit comments

Comments
 (0)