結果
問題 | No.1639 最小通信路 |
ユーザー |
|
提出日時 | 2021-06-25 02:18:36 |
言語 | Kotlin (2.1.0) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 43 |
ソースコード
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