結果

問題 No.1639 最小通信路
ユーザー 👑 箱星箱星
提出日時 2021-06-25 00:21:53
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 413 ms / 2,000 ms
コード長 1,659 bytes
コンパイル時間 18,008 ms
コンパイル使用メモリ 443,612 KB
実行使用メモリ 54,488 KB
最終ジャッジ日時 2023-10-17 02:20:23
合計ジャッジ時間 33,159 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
51,756 KB
testcase_01 AC 286 ms
51,756 KB
testcase_02 AC 301 ms
51,864 KB
testcase_03 AC 300 ms
51,844 KB
testcase_04 AC 413 ms
54,488 KB
testcase_05 AC 294 ms
51,780 KB
testcase_06 AC 299 ms
51,812 KB
testcase_07 AC 293 ms
51,768 KB
testcase_08 AC 297 ms
51,860 KB
testcase_09 AC 287 ms
51,768 KB
testcase_10 AC 293 ms
51,780 KB
testcase_11 AC 295 ms
51,804 KB
testcase_12 AC 295 ms
51,780 KB
testcase_13 AC 298 ms
51,828 KB
testcase_14 AC 288 ms
51,744 KB
testcase_15 AC 291 ms
51,772 KB
testcase_16 AC 298 ms
51,828 KB
testcase_17 AC 304 ms
51,860 KB
testcase_18 AC 293 ms
51,764 KB
testcase_19 AC 303 ms
51,796 KB
testcase_20 AC 307 ms
51,856 KB
testcase_21 AC 290 ms
51,752 KB
testcase_22 AC 294 ms
51,772 KB
testcase_23 AC 297 ms
51,804 KB
testcase_24 AC 291 ms
51,776 KB
testcase_25 AC 297 ms
51,776 KB
testcase_26 AC 295 ms
51,760 KB
testcase_27 AC 287 ms
51,764 KB
testcase_28 AC 289 ms
51,760 KB
testcase_29 AC 292 ms
51,792 KB
testcase_30 AC 296 ms
51,828 KB
testcase_31 AC 295 ms
51,776 KB
testcase_32 AC 296 ms
51,792 KB
testcase_33 AC 285 ms
51,760 KB
testcase_34 AC 288 ms
51,808 KB
testcase_35 AC 307 ms
51,868 KB
testcase_36 AC 307 ms
51,856 KB
testcase_37 AC 287 ms
51,808 KB
testcase_38 AC 290 ms
51,760 KB
testcase_39 AC 282 ms
51,764 KB
testcase_40 AC 283 ms
51,768 KB
testcase_41 AC 280 ms
51,756 KB
testcase_42 AC 286 ms
51,804 KB
testcase_43 AC 278 ms
51,748 KB
testcase_44 AC 280 ms
51,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve() {
    val n = nextInt()
    val uf = UnionFind(n)
    for (i in 0 until n * (n - 1) / 2) {
        val a = nextInt() - 1
        val b = nextInt() - 1
        val c = next()
        uf.unite(a, b)
        if (uf.size(0) == n) {
            println(c)
            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