結果

問題 No.1639 最小通信路
ユーザー 👑 箱星箱星
提出日時 2021-06-25 02:18:36
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 686 ms / 2,000 ms
コード長 2,979 bytes
コンパイル時間 14,945 ms
コンパイル使用メモリ 449,792 KB
実行使用メモリ 61,200 KB
最終ジャッジ日時 2023-10-17 02:22:23
合計ジャッジ時間 34,990 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 288 ms
51,920 KB
testcase_01 AC 295 ms
51,924 KB
testcase_02 AC 621 ms
61,088 KB
testcase_03 AC 686 ms
61,200 KB
testcase_04 AC 586 ms
61,104 KB
testcase_05 AC 366 ms
54,316 KB
testcase_06 AC 458 ms
57,472 KB
testcase_07 AC 292 ms
51,936 KB
testcase_08 AC 553 ms
61,052 KB
testcase_09 AC 328 ms
52,064 KB
testcase_10 AC 349 ms
54,316 KB
testcase_11 AC 501 ms
60,812 KB
testcase_12 AC 356 ms
54,308 KB
testcase_13 AC 608 ms
61,036 KB
testcase_14 AC 287 ms
51,928 KB
testcase_15 AC 346 ms
54,324 KB
testcase_16 AC 500 ms
60,960 KB
testcase_17 AC 558 ms
61,048 KB
testcase_18 AC 325 ms
52,056 KB
testcase_19 AC 386 ms
54,484 KB
testcase_20 AC 561 ms
61,040 KB
testcase_21 AC 281 ms
51,932 KB
testcase_22 AC 345 ms
54,316 KB
testcase_23 AC 448 ms
58,032 KB
testcase_24 AC 344 ms
54,320 KB
testcase_25 AC 350 ms
54,252 KB
testcase_26 AC 320 ms
54,152 KB
testcase_27 AC 297 ms
51,968 KB
testcase_28 AC 286 ms
51,944 KB
testcase_29 AC 450 ms
60,148 KB
testcase_30 AC 481 ms
60,964 KB
testcase_31 AC 340 ms
54,252 KB
testcase_32 AC 457 ms
60,556 KB
testcase_33 AC 332 ms
54,172 KB
testcase_34 AC 451 ms
60,028 KB
testcase_35 AC 536 ms
61,056 KB
testcase_36 AC 448 ms
59,792 KB
testcase_37 AC 469 ms
60,164 KB
testcase_38 AC 296 ms
51,960 KB
testcase_39 AC 357 ms
54,312 KB
testcase_40 AC 310 ms
52,016 KB
testcase_41 AC 326 ms
54,184 KB
testcase_42 AC 483 ms
60,232 KB
testcase_43 AC 327 ms
54,180 KB
testcase_44 AC 295 ms
51,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.lang.Exception
import java.util.*

fun PrintWriter.solve() {
    val n = nextInt()
    val m = n * (n - 1) / 2
    if (n !in 2..100) {
        throw Exception("2<=n<=100")
    }
    val a = Array(m) { 0 }
    val b = Array(m) { 0 }
    val c = Array(m) { 0.toBigInteger() }
    for (i in 0 until m) {
        val x = nextInt()
        val y = nextInt()
        if (x !in 1..n) {
            throw Exception("1<=a[i]<=n")
        }
        if (y !in 1..n) {
            throw Exception("1<=b[i]<=n")
        }
        val z = next()
        a[i] = x - 1
        b[i] = y - 1
        c[i] = z.toBigInteger()
        if (c[i] !in 1.toBigInteger().."10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".toBigInteger()
        ) {
            throw Exception("1<=c[i]<=10^100")
        }
        if (a[i] == b[i]) {
            throw Exception("a[i]!=b[i]")
        }
    }
    for (i in 1 until m) {
        if (c[i - 1] >= c[i]) {
            throw Exception("c[i-1]<c[i]")
        }
    }
    for (i in 0 until m) {
        for (j in 0 until i) {
            if (a[i] == a[j] && b[i] == b[j]) {
                throw Exception("(a[i],b[i]) distinct")
            }
            if (a[i] == b[j] && a[j] == b[i]) {
                throw Exception("(a[i],b[i]) distinct")
            }
        }
    }
    val uf = UnionFind(n)
    for (i in 0 until m) {
        uf.unite(a[i], b[i])
        if (uf.size(0) == n) {
            println(c[i])
            return
        }
    }
}

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

    fun find(x: Int): Int {
        if (parent[x] < 0) {
            return x
        }
        parent[x] = find(parent[x])
        return parent[x]
    }

    fun unite(x: Int, y: Int) {
        val x1 = find(x)
        val y1 = find(y)
        if (x1 == y1) {
            return
        }
        if (rank[x1] < rank[y1]) {
            parent[y1] += parent[x1]
            parent[x1] = y1
        } else {
            parent[x1] += parent[y1]
            parent[y1] = x1
            if (rank[x1] == rank[y1]) {
                rank[x1]++
            }
        }
    }

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

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

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0