結果

問題 No.1390 Get together
ユーザー tzyvrntzyvrn
提出日時 2021-02-12 23:20:26
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 2,822 bytes
コンパイル時間 15,105 ms
コンパイル使用メモリ 423,592 KB
実行使用メモリ 77,776 KB
最終ジャッジ日時 2023-09-27 06:49:01
合計ジャッジ時間 27,503 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 313 ms
53,888 KB
testcase_01 AC 307 ms
53,816 KB
testcase_02 AC 308 ms
53,732 KB
testcase_03 AC 516 ms
56,968 KB
testcase_04 AC 516 ms
56,796 KB
testcase_05 AC 524 ms
57,112 KB
testcase_06 AC 512 ms
56,952 KB
testcase_07 AC 474 ms
57,000 KB
testcase_08 AC 506 ms
57,120 KB
testcase_09 AC 508 ms
57,100 KB
testcase_10 AC 313 ms
53,636 KB
testcase_11 AC 311 ms
53,644 KB
testcase_12 AC 312 ms
54,184 KB
testcase_13 AC 322 ms
53,916 KB
testcase_14 AC 316 ms
54,004 KB
testcase_15 AC 321 ms
53,732 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*

fun main() {
    val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val slimes = List(n) { Pair(sc.nextInt() - 1, sc.nextInt() - 1) }

    val dsu = DSU(m)
    val color_leaders = MutableList(n) { -1 }
    for ((box, color) in slimes) {
        if (color_leaders[color] == -1) {
            color_leaders[color] = box
        } else {
            dsu.merge(box, color_leaders[color])
        }
    }

    val seen = mutableListOf<Int>()
    var ans = 0
    for (i in 0 until m) {
        if (seen.contains(dsu.leader(i))) {
            continue
        }
        ans += dsu.size(i) - 1
        seen.add(dsu.leader(i))
    }
    println(ans)
}

fun eprintln(obj: Any) = System.err.println(obj.toString())

/**
 * Disjoint set union.
 *
 * convert from [AtCoderLibraryForJava - DSU](https://github.com/NASU41/AtCoderLibraryForJava/blob/8b3a3b599790a9ca8c7dd92e036af4ddc5be1b05/DSU/DSU.java)
 */
class DSU(private val n: Int) {
    private val parentOrSize: IntArray = IntArray(n) { -1 }

    /**
     * Merge nodes.
     */
    fun merge(a: Int, b: Int): Int {
        if (a !in 0 until n) throw IndexOutOfBoundsException("a=$a")
        if (b !in 0 until n) throw IndexOutOfBoundsException("b=$b")
        var x = leader(a)
        var y = leader(b)
        if (x == y) return x
        if (-parentOrSize[x] < -parentOrSize[y]) {
            val tmp = x
            x = y
            y = tmp
        }
        parentOrSize[x] += parentOrSize[y]
        parentOrSize[y] = x
        return x
    }

    /**
     * True if two nodes are connected.
     */
    fun same(a: Int, b: Int): Boolean {
        if (a !in 0 until n) throw IndexOutOfBoundsException("a=$a")
        if (b !in 0 until n) throw IndexOutOfBoundsException("b=$b")
        return leader(a) == leader(b)
    }

    /**
     * Get its leader node.
     */
    fun leader(a: Int): Int {
        return if (parentOrSize[a] < 0) {
            a
        } else {
            parentOrSize[a] = leader(parentOrSize[a])
            parentOrSize[a]
        }
    }

    /**
     * A group's size.
     */
    fun size(a: Int): Int {
        if (a !in 0 until n) throw IndexOutOfBoundsException("" + a)
        return -parentOrSize[leader(a)]
    }

    /**
     * Group by leader.
     */
    fun groups(): ArrayList<ArrayList<Int>> {
        val leaderBuf = IntArray(n)
        val groupSize = IntArray(n)
        for (i in 0 until n) {
            leaderBuf[i] = leader(i)
            groupSize[leaderBuf[i]]++
        }
        val result = ArrayList<ArrayList<Int>>(n)
        for (i in 0 until n) {
            result.add(ArrayList(groupSize[i]))
        }
        for (i in 0 until n) {
            result[leaderBuf[i]].add(i)
        }
        result.removeIf { it.isEmpty() }
        return result
    }
}
0