-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path[백준] 박정아_줄 세우기.swift
42 lines (34 loc) · 1.06 KB
/
[백준] 박정아_줄 세우기.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
import Foundation
func solution() {
let input = readLine()!.split(separator: " ").map{ Int(String($0))! }
let n = input[0]
let m = input[1]
var graph = Array(repeating: Array(repeating: 0, count: 0), count: n + 1)
var degree = Array(repeating: 0, count: n + 1)
var queue = [Int]()
var answer = [Int]()
for _ in 0..<m {
let students = readLine()!.split(separator: " ").map{ Int(String($0))! }
graph[students[0]].append(students[1])
degree[students[1]] += 1
}
degree.enumerated().forEach { student, count in
if student != 0 && count == 0 {
queue.append(student)
}
}
while !queue.isEmpty {
let student = queue.removeFirst()
answer.append(student)
graph[student].forEach { nextStudent in
degree[nextStudent] -= 1
if degree[nextStudent] == 0 {
queue.append(nextStudent)
}
}
}
answer.forEach { number in
print(number, terminator: " ")
}
}
solution()