結果

問題 No.1473 おでぶなおばけさん
ユーザー 👑 箱星箱星
提出日時 2021-07-03 20:30:51
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 2,469 bytes
コンパイル時間 16,412 ms
コンパイル使用メモリ 434,640 KB
実行使用メモリ 90,216 KB
最終ジャッジ日時 2023-09-13 01:59:40
合計ジャッジ時間 56,670 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
52,880 KB
testcase_01 AC 281 ms
53,028 KB
testcase_02 AC 918 ms
78,072 KB
testcase_03 AC 796 ms
71,496 KB
testcase_04 AC 645 ms
63,300 KB
testcase_05 AC 518 ms
58,552 KB
testcase_06 AC 830 ms
69,508 KB
testcase_07 AC 1,125 ms
90,216 KB
testcase_08 AC 1,141 ms
90,188 KB
testcase_09 AC 1,083 ms
89,932 KB
testcase_10 AC 755 ms
63,756 KB
testcase_11 AC 766 ms
63,268 KB
testcase_12 AC 738 ms
63,268 KB
testcase_13 AC 659 ms
60,840 KB
testcase_14 AC 564 ms
59,060 KB
testcase_15 AC 705 ms
64,140 KB
testcase_16 AC 732 ms
63,988 KB
testcase_17 AC 417 ms
58,416 KB
testcase_18 AC 436 ms
58,280 KB
testcase_19 AC 620 ms
62,740 KB
testcase_20 AC 745 ms
66,968 KB
testcase_21 AC 821 ms
69,144 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 827 ms
78,024 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 724 ms
77,372 KB
testcase_32 AC 626 ms
68,900 KB
testcase_33 AC 619 ms
66,784 KB
testcase_34 AC 505 ms
60,460 KB
testcase_35 AC 530 ms
64,660 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 748 ms
62,996 KB
testcase_40 AC 753 ms
63,256 KB
testcase_41 AC 720 ms
63,772 KB
testcase_42 AC 771 ms
63,052 KB
testcase_43 AC 684 ms
69,012 KB
testcase_44 AC 677 ms
69,196 KB
testcase_45 AC 674 ms
69,484 KB
testcase_46 AC 783 ms
71,524 KB
testcase_47 AC 956 ms
73,320 KB
testcase_48 AC 849 ms
73,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextInt()
    val data = mutableListOf<Triple<Int, Int, Int>>()
    val adj = Array(n) { mutableListOf<Int>() }
    val uf = UnionFind(n)
    for (i in 0 until m) {
        val s = nextInt() - 1
        val t = nextInt() - 1
        val d = nextInt()
        data.add(Triple(s, t, d))
    }
    data.sortByDescending { it.third }
    for ((s, t, d) in data) {
        uf.unite(s, t)
        adj[s].add(t)
        adj[t].add(s)
        if (uf.same(0, n - 1)) {
            val que: Queue<Pair<Int, Int>> = ArrayDeque()
            que.add(0 to 0)
            val visited = BooleanArray(n) { false }
            visited[0] = true
            while (que.isNotEmpty()) {
                val (v, num) = que.poll()
                if (v == n - 1) {
                    println("$d $num")
                    return
                }
                for (w in adj[v]) {
                    if (!visited[w]) {
                        que.add(w to num + 1)
                        visited[w] = true
                    }
                }
            }
        }
    }
}

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