// I'm not sure if this should pass, but _drf version probably should fun main() { System.setIn(System.`in`.buffered()) val (n, m) = readInts() val g = List(n) { mutableListOf() } val rg = List(n) { mutableListOf() } repeat(m) { val (a, b) = readInts() g[a - 1].add(b - 1) rg[b - 1].add(a - 1) } val topSort = mutableListOf() val used = BooleanArray(n) { false } for (i in 0 until n) { if (!used[i]) { fun dfs(v: Int) { used[v] = true for (v in g[v]) { if (!used[v]) { dfs(v) } } topSort.add(v) } dfs(i) } } val color = IntArray(n) { -1 } var cc = 0 for (i in topSort.reversed()) { if (color[i] == -1) { fun dfs(v: Int) { color[v] = cc for (v in rg[v]) { if (color[v] == -1) { dfs(v) } } } dfs(i) cc++ } } val cs = BooleanArray(cc) { true } for (v in g.indices) { for (to in g[v]) { if (color[to] != color[v]) { cs[color[to]] = false } } } println(cs.count { it }) } private fun readStrings() = readln().split(" ") private fun readInts() = readStrings().map { it.toInt() }