-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path026.swift
49 lines (41 loc) · 885 Bytes
/
026.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
func readInt() -> Int {
Int(readLine()!)!
}
func readInts() -> [Int] {
readLine()!.split(separator: " ").map { Int(String($0))! }
}
let n = readInt()
var e = [[Int]](repeating: [], count: n + 1)
for _ in 1...n - 1 {
let edge = readInts()
e[edge[0]].append(edge[1])
e[edge[1]].append(edge[0])
}
var col = [Int](repeating: -1, count: n + 1)
var count1 = 0
var count2 = 0
func dfs(pos: Int, cur: Int) {
col[pos] = cur
if cur == 1 {
count1 += 1
} else {
count2 += 1
}
for i in e[pos] {
if col[i] == -1 {
dfs(pos: i, cur: 3 - cur)
}
}
}
dfs(pos: 1, cur: 1)
var printVal = count1 >= count2 ? 1 : 2
var printCounter = 0
for i in 1...n {
if col[i] == printVal {
print(i, terminator: " ")
printCounter += 1
if printCounter == n / 2 {
break
}
}
}