Kotlin DFS
\nclass Solution {\n  fun minReorder(n: Int, connections: Array<IntArray>): Int {\n    val g = Array<MutableList<Pair<Int, Boolean>>>(n) { mutableListOf<Pair<In
```\nclass Solution {\n  fun minReorder(n: Int, connections: Array<IntArray>): Int {\n    val g = Array<MutableList<Pair<Int, Boolean>>>(n) { mutableListOf<Pair<Int, Boolean>>() }\n    for ((a, b) in connections) {\n      g[a].add(Pair(b, true)) // this edge is provided\n      g[b].add(Pair(a, false)) // this edge is imagined\n    }\n\n    fun dfs(parent: Int, node: Int): Int {\n      var res = 0\n      for ((next, provided) in g[node]) {\n        if (next == parent) continue\n        if (provided) // we need to reverse it\n          res++\n        res += dfs(node, next)\n      }\n      return res\n    }\n    return dfs(-1, 0)\n  }\n}\n```
['Kotlin']