結果

問題 No.556 仁義なきサルたち
ユーザー rutilicusrutilicus
提出日時 2020-10-14 20:53:02
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 443 ms / 2,000 ms
コード長 2,004 bytes
コンパイル時間 14,891 ms
コンパイル使用メモリ 419,840 KB
実行使用メモリ 57,848 KB
最終ジャッジ日時 2023-09-28 00:51:04
合計ジャッジ時間 23,754 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 264 ms
52,780 KB
testcase_01 AC 272 ms
53,000 KB
testcase_02 AC 272 ms
52,788 KB
testcase_03 AC 260 ms
52,808 KB
testcase_04 AC 261 ms
52,760 KB
testcase_05 AC 276 ms
52,908 KB
testcase_06 AC 270 ms
52,864 KB
testcase_07 AC 279 ms
52,800 KB
testcase_08 AC 283 ms
52,816 KB
testcase_09 AC 285 ms
53,472 KB
testcase_10 AC 297 ms
53,304 KB
testcase_11 AC 299 ms
53,264 KB
testcase_12 AC 298 ms
53,076 KB
testcase_13 AC 292 ms
53,188 KB
testcase_14 AC 365 ms
54,412 KB
testcase_15 AC 384 ms
56,680 KB
testcase_16 AC 407 ms
56,596 KB
testcase_17 AC 425 ms
57,204 KB
testcase_18 AC 435 ms
57,796 KB
testcase_19 AC 439 ms
57,732 KB
testcase_20 AC 443 ms
57,848 KB
testcase_21 AC 432 ms
57,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:84:17: warning: 'appendln(Int): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
        builder.appendln(uf.getTop(it) + 1)
                ^

ソースコード

diff #

class UnionFind(n: Int) {
    private val parent = IntArray(n) { -1 }
    private val rank = IntArray(n)
    private val size = IntArray(n) { 1 }
    private var numTree = n

    private val top = IntArray(n) { it }

    fun find(x: Int): Int {
        if (parent[x] == -1) {
            return x
        } else {
            parent[x] = find(parent[x])
            return parent[x]
        }
    }

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

    fun getSize(): Int {
        return numTree
    }

    fun sizeOf(x: Int): Int {
        return size[find(x)]
    }

    fun rankOf(x: Int): Int {
        return rank[find(x)]
    }

    fun getTop(x: Int): Int {
        return top[find(x)]
    }

    fun unite(x: Int, y: Int) {
        val parentX = find(x)
        val parentY = find(y)

        if (parentX == parentY) {
            return
        }

        numTree--

        if (sizeOf(parentX) < sizeOf(parentY)) {
            top[parentX] = top[parentY]
        } else if(sizeOf(parentX) > sizeOf(parentY)) {
            top[parentY] = top[parentX]
        } else if (top[parentX] < top[parentY]) {
            top[parentY] = top[parentX]
        } else {
            top[parentX] = top[parentY]
        }

        if (rank[parentX] < rank[parentY]) {
            parent[parentX] = parentY
            size[parentY] += size[parentX]
        } else {
            parent[parentY] = parentX
            size[parentX] += size[parentY]
            if (rank[parentX] == rank[parentY]) {
                rank[parentX]++
            }
        }
    }
}

fun main() {
    val builder = StringBuilder()

    val (n, m) = readInputLine().split(" ").map { it.toInt() }

    val uf = UnionFind(n)

    repeat(m) {
        val (a, b) = readInputLine().split(" ").map { it.toInt() - 1 }
        uf.unite(a, b)
    }

    repeat(n) {
        builder.appendln(uf.getTop(it) + 1)
    }

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}
0