結果

問題 No.1639 最小通信路
ユーザー 👑 箱
提出日時 2021-06-25 00:18:34
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 14,324 ms
コンパイル使用メモリ 447,388 KB
実行使用メモリ 54,552 KB
最終ジャッジ日時 2023-10-17 02:21:05
合計ジャッジ時間 29,748 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 269 ms
51,816 KB
testcase_01 AC 273 ms
51,816 KB
testcase_02 AC 288 ms
51,936 KB
testcase_03 AC 285 ms
51,908 KB
testcase_04 AC 384 ms
54,552 KB
testcase_05 AC 278 ms
51,840 KB
testcase_06 AC 279 ms
51,880 KB
testcase_07 AC 264 ms
51,816 KB
testcase_08 AC 306 ms
52,840 KB
testcase_09 AC 266 ms
51,828 KB
testcase_10 AC 269 ms
51,840 KB
testcase_11 AC 272 ms
51,868 KB
testcase_12 AC 276 ms
51,840 KB
testcase_13 AC 283 ms
51,896 KB
testcase_14 AC 270 ms
51,812 KB
testcase_15 AC 293 ms
51,836 KB
testcase_16 AC 313 ms
52,800 KB
testcase_17 AC 293 ms
51,948 KB
testcase_18 AC 275 ms
51,824 KB
testcase_19 AC 289 ms
51,880 KB
testcase_20 AC 299 ms
51,940 KB
testcase_21 AC 279 ms
51,808 KB
testcase_22 AC 276 ms
51,844 KB
testcase_23 AC 280 ms
51,860 KB
testcase_24 AC 274 ms
51,832 KB
testcase_25 AC 282 ms
51,832 KB
testcase_26 AC 273 ms
51,824 KB
testcase_27 AC 274 ms
51,824 KB
testcase_28 AC 275 ms
51,812 KB
testcase_29 AC 281 ms
51,852 KB
testcase_30 AC 301 ms
51,908 KB
testcase_31 AC 282 ms
51,840 KB
testcase_32 AC 290 ms
51,872 KB
testcase_33 AC 282 ms
51,832 KB
testcase_34 AC 285 ms
51,872 KB
testcase_35 AC 327 ms
52,824 KB
testcase_36 AC 298 ms
51,928 KB
testcase_37 AC 293 ms
51,876 KB
testcase_38 AC 278 ms
51,820 KB
testcase_39 AC 283 ms
51,848 KB
testcase_40 AC 277 ms
51,824 KB
testcase_41 AC 281 ms
51,828 KB
testcase_42 AC 294 ms
51,900 KB
testcase_43 AC 279 ms
51,828 KB
testcase_44 AC 276 ms
51,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val uf = UnionFind(n)
    loop@for (i in 0 until n * (n - 1) / 2) {
        val a = nextInt() - 1
        val b = nextInt() - 1
        val c = next()
        uf.unite(a, b)
        for (j in 0 until n) {
            for (k in 0 until j) {
                if (!uf.same(j, k)) {
                    continue@loop
                }
            }
        }
        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