just a variant
Code\nKotlin []\nclass Solution {\n    fun maxNumEdgesToRemove(n: Int, edges: Array<IntArray>): Int {\n        val dsBoth = IntArray(n+1) {-1}\n        var used
# Code\n```Kotlin []\nclass Solution {\n    fun maxNumEdgesToRemove(n: Int, edges: Array<IntArray>): Int {\n        val dsBoth = IntArray(n+1) {-1}\n        var used = 0\n\n        fun find(ds: IntArray, i: Int): Int = if (ds[i] < 0) i else find(ds, ds[i]).also { ds[i] = it }\n\n        (3 downTo 1).forEach { tp ->\n            val ds = if (tp == 3) dsBoth else dsBoth.clone()\n            edges.forEach { e ->\n                if (e[0] == tp) {\n                    var i = find(ds, e[1])\n                    var j = find(ds, e[2])\n                    if (i != j) {\n                        used++\n                        if (ds[j] < ds[i]) i = j.also { j = i }\n                        ds[i] += ds[j]\n                        ds[j] = i\n                    }\n                }\n            }\n            if (tp != 3 && ds[find(ds,1)] != -n) return -1\n        }\n        return edges.size - used\n    }\n}\n```
['Kotlin']