-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path003.swift
More file actions
51 lines (43 loc) · 922 Bytes
/
003.swift
File metadata and controls
51 lines (43 loc) · 922 Bytes
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
func readInt() -> Int {
Int(readLine()!)!
}
func readInts() -> [Int] {
readLine()!.split(separator: " ").map { Int(String($0))! }
}
let n = readInt()
var edges = [[Int]](repeating: [], count: n)
for _ in 1...n - 1 {
let pair = readInts()
//1を引いて0開始にします
edges[pair[0] - 1].append(pair[1] - 1)
edges[pair[1] - 1].append(pair[0] - 1)
}
var depth: [Int] = []
var seen: [Bool] = []
var dMaxNode = -1
var dMax = -1
func prepare() {
depth = [Int](repeating: -1, count: n)
seen = [Bool](repeating: false, count: n)
dMaxNode = -1
dMax = -1
}
func dfs(_ node: Int, _ d: Int) {
seen[node] = true
depth[node] = d
if d > dMax {
dMax = d
dMaxNode = node
}
for e in edges[node] {
if seen[e] == false {
dfs(e, d + 1)
}
}
}
prepare()
dfs(0, 0)
let next = dMaxNode
prepare()
dfs(next, 0)
print(dMax + 1)