-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path062.swift
More file actions
45 lines (39 loc) · 773 Bytes
/
062.swift
File metadata and controls
45 lines (39 loc) · 773 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
func readInt() -> Int {
Int(readLine()!)!
}
func readIntArray() -> [Int] {
readLine()!.split(separator: " ").map { Int(String($0))! }
}
let n = readInt()
var outArrow = [[Int]](repeating: [], count: n + 1)
var selected = [Bool](repeating: false, count: n + 1)
var order: [Int] = []
for i in 1...n {
let input = readIntArray()
if input[0] == i || input[1] == i {
order.append(i)
selected[i] = true
} else {
outArrow[input[0]].append(i)
if input[0] != input[1] {
outArrow[input[1]].append(i)
}
}
}
var walk = 0
while walk < order.count {
for o in outArrow[order[walk]] {
if selected[o] == false {
order.append(o)
selected[o] = true
}
}
walk += 1
}
if order.count == n {
for or in order.reversed() {
print(or)
}
} else {
print(-1)
}