結果
問題 | No.1639 最小通信路 |
ユーザー | 箱星 |
提出日時 | 2021-06-25 02:18:36 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 576 ms / 2,000 ms |
コード長 | 2,979 bytes |
コンパイル時間 | 14,820 ms |
コンパイル使用メモリ | 450,256 KB |
実行使用メモリ | 63,888 KB |
最終ジャッジ日時 | 2024-09-17 00:56:19 |
合計ジャッジ時間 | 29,206 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 248 ms
49,980 KB |
testcase_01 | AC | 260 ms
49,660 KB |
testcase_02 | AC | 540 ms
59,412 KB |
testcase_03 | AC | 576 ms
63,676 KB |
testcase_04 | AC | 508 ms
63,888 KB |
testcase_05 | AC | 314 ms
51,304 KB |
testcase_06 | AC | 407 ms
56,160 KB |
testcase_07 | AC | 265 ms
50,032 KB |
testcase_08 | AC | 501 ms
58,076 KB |
testcase_09 | AC | 290 ms
50,252 KB |
testcase_10 | AC | 309 ms
50,952 KB |
testcase_11 | AC | 430 ms
56,864 KB |
testcase_12 | AC | 305 ms
50,984 KB |
testcase_13 | AC | 525 ms
59,328 KB |
testcase_14 | AC | 252 ms
49,976 KB |
testcase_15 | AC | 300 ms
50,672 KB |
testcase_16 | AC | 432 ms
57,136 KB |
testcase_17 | AC | 511 ms
58,084 KB |
testcase_18 | AC | 279 ms
50,236 KB |
testcase_19 | AC | 353 ms
51,776 KB |
testcase_20 | AC | 522 ms
58,068 KB |
testcase_21 | AC | 260 ms
49,964 KB |
testcase_22 | AC | 319 ms
50,948 KB |
testcase_23 | AC | 416 ms
55,948 KB |
testcase_24 | AC | 301 ms
51,148 KB |
testcase_25 | AC | 306 ms
51,040 KB |
testcase_26 | AC | 283 ms
50,340 KB |
testcase_27 | AC | 262 ms
50,184 KB |
testcase_28 | AC | 250 ms
49,724 KB |
testcase_29 | AC | 385 ms
56,904 KB |
testcase_30 | AC | 428 ms
58,844 KB |
testcase_31 | AC | 302 ms
50,872 KB |
testcase_32 | AC | 395 ms
56,660 KB |
testcase_33 | AC | 310 ms
50,388 KB |
testcase_34 | AC | 407 ms
56,376 KB |
testcase_35 | AC | 473 ms
61,364 KB |
testcase_36 | AC | 398 ms
56,688 KB |
testcase_37 | AC | 412 ms
56,692 KB |
testcase_38 | AC | 258 ms
49,988 KB |
testcase_39 | AC | 315 ms
51,184 KB |
testcase_40 | AC | 286 ms
50,052 KB |
testcase_41 | AC | 300 ms
50,460 KB |
testcase_42 | AC | 445 ms
58,504 KB |
testcase_43 | AC | 292 ms
50,428 KB |
testcase_44 | AC | 260 ms
50,212 KB |
ソースコード
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