結果

問題 No.556 仁義なきサルたち
ユーザー rutilicusrutilicus
提出日時 2020-10-14 20:47:07
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 1,994 bytes
コンパイル時間 18,156 ms
コンパイル使用メモリ 426,684 KB
実行使用メモリ 59,384 KB
最終ジャッジ日時 2023-09-28 00:49:03
合計ジャッジ時間 24,779 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
52,944 KB
testcase_01 AC 266 ms
52,920 KB
testcase_02 AC 264 ms
52,972 KB
testcase_03 AC 260 ms
53,072 KB
testcase_04 AC 267 ms
53,288 KB
testcase_05 AC 270 ms
52,968 KB
testcase_06 AC 270 ms
52,844 KB
testcase_07 AC 273 ms
52,944 KB
testcase_08 AC 282 ms
52,960 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 305 ms
53,424 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 432 ms
59,384 KB
testcase_20 AC 426 ms
57,780 KB
testcase_21 AC 433 ms
57,960 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 (parentX < 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