-
[Swift] ๋ฐฑ์ค 2606 ๋ฐ์ด๋ฌ์ค (Graph Traversal)๐ป Algorithm/Swift 2023. 1. 31. 22:44
๐ ํ์ด
๐ฌ Code
let n = Int(readLine()!)! let m = Int(readLine()!)! var graph = Array<[Int]>(repeating: [], count: n+1) var visited = Array<Bool>(repeating: false, count: n+1) for _ in 0..<m { let input = readLine()!.split(separator: " ").map { Int(String($0))! } graph[input[0]].append(input[1]) graph[input[1]].append(input[0]) } func dfs(start: Int) { visited[start] = true for i in graph[start] { if !visited[i] { dfs(start: i) } } } dfs(start: 1) print(visited.filter{$0}.count - 1)
'๐ป Algorithm > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ๋ฐฑ์ค 2667 ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (Graph Traversal) (0) 2023.01.31 [Swift] ๋ฐฑ์ค 9012 ๊ดํธ (Data Structure) (0) 2023.01.31 [Swift] ๋ฐฑ์ค 1260 DFS์ BFS (Graph Traversal) (0) 2023.01.31 [Swift] ๋ฐฑ์ค 14490 ๋ฐฑ๋์ด (์ต๋๊ณต์ฝ์ GCM ๋ฌธ์ ) (0) 2023.01.31 [Swift] ๋ฐฑ์ค 2941 ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (String) (0) 2023.01.31