HIT해
[Swift] 백준 - 바이러스 본문
728x90
https://www.acmicpc.net/problem/2606
import Foundation
func main() {
// 언래핑
guard let V = Int(readLine()!), let N = Int(readLine()!) else { return }
var graph = [[Int]](repeating: [], count: V+1)
for _ in 0..<N{
let XY = readLine()!.split(separator: " ").map { Int($0)! }
let x = XY[0]
let y = XY[1]
graph[x].append(y)
graph[y].append(x)
}
func bfs() {
var visited = [Bool](repeating: false, count: V+1)
var queue = [Int]()
var result = 0
queue.append(1)
while !queue.isEmpty{
let node = queue.removeFirst()
if !visited[node]{
visited[node] = true
result += 1
for i in graph[node]{
queue.append(i)
}
}
}
print(result-1)
}
bfs()
}
// main 함수 실행
main()
'Swift > Swift 알고리즘' 카테고리의 다른 글
[Swift] 백준 - 큐 2 (0) | 2024.09.10 |
---|---|
[Swift] 백준 - 미로찾기 (0) | 2024.09.10 |
[Swift] 백준 - 반복수열 (0) | 2024.09.09 |
[Swift] 프로그래머스 - 타겟 넘버 ( DFS ) (0) | 2024.09.09 |
[Swift] 백준 - DFS와 BFS (0) | 2024.09.08 |