baekjoon 20251201

This commit is contained in:
songyc macbook 2025-12-01 21:02:17 +09:00
parent e8ba701e3a
commit 11abda6b56

View File

@ -0,0 +1,36 @@
if let input1 = readLine(),
let NM = input1.split(separator: " ").compactMap({Int($0)}) as? [Int],
let N = NM.first, let M = NM.last
{
var graph: [[Int]] = Array(repeating: [], count: N+1)
var inDegree = Array(repeating: 0, count: N+1)
for _ in 1...M {
guard let input2 = readLine(),
let uv = input2.split(separator: " ").compactMap({Int($0)}) as? [Int],
let u = uv.first, let v = uv.last
else { break }
graph[u].append(v)
inDegree[v] += 1
}
var qu: [Int] = []
var idx = 0
for n in 1...N {
if inDegree[n] == 0 { qu.append(n) }
}
while idx < qu.count {
let num = qu[idx]
idx += 1
for nxt in graph[num] {
inDegree[nxt] -= 1
if inDegree[nxt] == 0 { qu.append(nxt) }
}
}
print(qu.map{ String($0) }.joined(separator: " "))
}