-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
59 lines (48 loc) · 1.6 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// main.swift
// DFS/DFS와_BFS_1260
//
// Created by Yongwoo Marco on 2021/08/28.
//
// https://www.acmicpc.net/problem/1260
let inputs = readLine()!.split(separator: " ").map({Int(String($0))!})
var vertex = [[Int]](repeating: [], count: inputs[0]+1)
for _ in 0..<inputs[1] {
let nodes = readLine()!.split(separator: " ").map({Int(String($0))!})
vertex[nodes[0]].append(nodes[1])
vertex[nodes[0]].sort(by: <)
vertex[nodes[1]].append(nodes[0])
vertex[nodes[1]].sort(by: <)
}
func dfs(base: Int, visited: inout [Bool], _ result: inout [Int], _ vertex: inout [[Int]]) {
if visited[base] {
return
} else {
visited[base] = true
result.append(base)
vertex[base].forEach { next in
dfs(base: next, visited: &visited, &result, &vertex)
}
}
}
func bfs(base: Int, _ result: inout [Int], _ vertex: inout [[Int]]) {
var willVisit = [base]
while let now = willVisit.popLast(), result.count < vertex.count - 1 {
result.append(now)
for next in vertex[now] {
if !willVisit.contains(next), !result.contains(next) {
willVisit.insert(next, at: 0)
}
}
}
}
var dfsVertex = vertex, bfsVertex = vertex
// MARK: --- DFS
var visited = [Bool](repeating: false, count: inputs[0] + 1)
var dfsResult = [Int]()
dfs(base: inputs[2], visited: &visited, &dfsResult, &dfsVertex)
print( dfsResult.map({ String($0) }).joined(separator: " "))
// MARK: --- BFS
var bfsResult = [Int]()
bfs(base: inputs[2], &bfsResult, &bfsVertex)
print(bfsResult.map({ String($0) }).joined(separator: " "))