From 11abda6b569164ef0ec6825907c9ca24117d7b49 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Mon, 1 Dec 2025 21:02:17 +0900 Subject: [PATCH] baekjoon 20251201 --- code_study/Baekjoon/swift/2252.swift | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 code_study/Baekjoon/swift/2252.swift diff --git a/code_study/Baekjoon/swift/2252.swift b/code_study/Baekjoon/swift/2252.swift new file mode 100644 index 0000000..5f9196d --- /dev/null +++ b/code_study/Baekjoon/swift/2252.swift @@ -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: " ")) +}