Submission #39658176


Source Code Expand

fun main() {
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }

    val diff = n
    val uf = UnionFind(n + diff + 1)

    for(i in 1..n) {
        uf.union(i, i + diff)
    }

    var x = 0
    repeat(m) {
        val (a, b, c, d) = readLine()!!.split(" ")

        val aa =if(b == "R") {
            a.toInt()
        } else {
            a.toInt() + diff
        }

        val cc = if(d == "R") {
            c.toInt()
        } else {
            c.toInt() + diff
        }

        if(uf.isSame(aa, cc)) {
            x++
        }

        uf.union(aa, cc)
    }
    val num = (1..n + diff).map { uf.find(it) }.distinct().size

    println("$x ${num - x}")
}

private class UnionFind(n: Int){
    private val roots = IntArray(n){ it }
    private val ranks = IntArray(n){ 1 }

    fun find(i: Int): Int{
        if(roots[i] != i){
            roots[i] = find(roots[i])
        }

        return roots[i]
    }

    fun isSame(x: Int, y: Int): Boolean {
        return find(x) == find(y)
    }

    fun union(x: Int, y: Int){
        val rootX = find(x)
        val rootY = find(y)

        if(rootX == rootY) {
            return
        }
        if(ranks[rootX] > ranks[rootY]){
            roots[rootY] = rootX
            ++ranks[rootX]
        }else{
            roots[rootX] = rootY
            ++ranks[rootY]
        }
    }
}

Submission Info

Submission Time
Task D - Tying Rope
User dhirabayashi
Language Kotlin (1.3.71)
Score 400
Code Size 1418 Byte
Status AC
Exec Time 935 ms
Memory 83192 KiB

Compile Error

warning: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+InlineClasses

This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 31
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt
Case Name Status Exec Time Memory
sample00.txt AC 110 ms 36336 KiB
sample01.txt AC 99 ms 36428 KiB
sample02.txt AC 104 ms 36316 KiB
testcase00.txt AC 782 ms 70868 KiB
testcase01.txt AC 806 ms 70992 KiB
testcase02.txt AC 285 ms 64896 KiB
testcase03.txt AC 848 ms 82520 KiB
testcase04.txt AC 473 ms 65348 KiB
testcase05.txt AC 505 ms 62428 KiB
testcase06.txt AC 502 ms 62136 KiB
testcase07.txt AC 294 ms 66256 KiB
testcase08.txt AC 513 ms 66384 KiB
testcase09.txt AC 880 ms 68052 KiB
testcase10.txt AC 805 ms 72152 KiB
testcase11.txt AC 773 ms 64180 KiB
testcase12.txt AC 840 ms 82604 KiB
testcase13.txt AC 512 ms 66256 KiB
testcase14.txt AC 761 ms 67136 KiB
testcase15.txt AC 935 ms 68056 KiB
testcase16.txt AC 862 ms 72688 KiB
testcase17.txt AC 689 ms 68956 KiB
testcase18.txt AC 500 ms 62328 KiB
testcase19.txt AC 857 ms 73792 KiB
testcase20.txt AC 833 ms 64220 KiB
testcase21.txt AC 746 ms 72796 KiB
testcase22.txt AC 704 ms 67136 KiB
testcase23.txt AC 533 ms 66292 KiB
testcase24.txt AC 790 ms 72268 KiB
testcase25.txt AC 748 ms 70584 KiB
testcase26.txt AC 819 ms 70508 KiB
testcase27.txt AC 763 ms 83192 KiB