-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
37 lines (27 loc) · 904 Bytes
/
main.swift
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
//
// main.swift
// DFS/바이러스_2606
//
// Created by Yongwoo Marco on 2021/08/18.
//
// https://www.acmicpc.net/problem/2606
import Foundation
let computerCount = Int(readLine()!)!
let networkCount = Int(readLine()!)!
var computers = [(Bool, Set<Int>)](repeating: (false, []), count: computerCount + 1)
for _ in 1...networkCount {
let input = readLine()!.components(separatedBy: " ").map({ Int($0)! })
computers[input[0]].1.update(with: input[1])
computers[input[1]].1.update(with: input[0])
}
func spread(from: Int, network: inout [(Bool, Set<Int>)]) {
print("\(from) 컴퓨터 방문 \(network[from].0)")
if network[from].0 == true {
return
}
network[from].0 = true
network[from].1.forEach({ spread(from: $0, network: &network) })
}
spread(from: 1, network: &computers)
let result = computers.filter({ $0.0 == true }).count
print(result - 1)