-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ1967.swift
More file actions
74 lines (59 loc) · 1.8 KB
/
BOJ1967.swift
File metadata and controls
74 lines (59 loc) · 1.8 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Foundation
protocol Readable {
func readSingleValue() -> String
func readArray(with separator: Character) -> [String]
}
extension Readable {
func readSingleValue() -> String {
return readLine()!
}
func readSingleValue() -> Int {
return Int(readLine()!)!
}
func readArray(with separator: Character) -> [String] {
return readLine()!.split(separator: " ").map { String($0) }
}
func readArray(with separator: Character) -> [Int] {
return readLine()!.split(separator: " ").map { Int(String($0))! }
}
}
class BOJ1967: Readable {
class Node {
var value: Int
var weight: Int
init(_ value: Int, weight: Int) {
self.value = value
self.weight = weight
}
}
func input() {
for _ in 0..<readSingleValue()-1 {
let n: [Int] = readArray(with: " ")
nodes[n[0]].append(Node(n[1], weight: n[2]))
nodes[n[1]].append(Node(n[0], weight: n[2]))
}
}
var nodes = [[Node]](repeating: [Node](), count: 10001)
var visited = [Bool](repeating: false, count: 10001)
var maxDiameter = 0
var farthestNodeFromRoot = 0
func solution() {
input()
dfs(node: 1, weight: 0)
visited = [Bool](repeating: false, count: 10001)
dfs(node: farthestNodeFromRoot, weight: 0)
print(maxDiameter)
}
func dfs(node: Int, weight: Int) {
guard !visited[node] else { return }
visited[node] = true
if weight > maxDiameter {
maxDiameter = weight
farthestNodeFromRoot = node
}
for adj in nodes[node] {
dfs(node: adj.value, weight: weight + adj.weight)
}
}
}
BOJ1967().solution()