結果
問題 | No.2291 Union Find Estimate |
ユーザー | 👑 箱 |
提出日時 | 2023-03-09 03:11:05 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 767 ms / 2,000 ms |
コード長 | 4,662 bytes |
コンパイル時間 | 22,258 ms |
コンパイル使用メモリ | 460,660 KB |
実行使用メモリ | 67,964 KB |
最終ジャッジ日時 | 2023-10-18 06:07:20 |
合計ジャッジ時間 | 29,825 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 298 ms
53,996 KB |
testcase_01 | AC | 299 ms
54,016 KB |
testcase_02 | AC | 767 ms
61,624 KB |
testcase_03 | AC | 455 ms
62,516 KB |
testcase_04 | AC | 375 ms
56,684 KB |
testcase_05 | AC | 371 ms
56,680 KB |
testcase_06 | AC | 367 ms
56,660 KB |
testcase_07 | AC | 319 ms
54,072 KB |
testcase_08 | AC | 378 ms
56,692 KB |
testcase_09 | AC | 381 ms
56,684 KB |
testcase_10 | AC | 505 ms
62,992 KB |
testcase_11 | AC | 546 ms
62,096 KB |
testcase_12 | AC | 617 ms
67,964 KB |
testcase_13 | AC | 442 ms
59,504 KB |
testcase_14 | AC | 482 ms
62,360 KB |
testcase_15 | AC | 485 ms
62,604 KB |
testcase_16 | AC | 456 ms
61,864 KB |
testcase_17 | AC | 434 ms
60,748 KB |
testcase_18 | AC | 433 ms
60,768 KB |
testcase_19 | AC | 462 ms
62,752 KB |
ソースコード
import java.io.PrintWriter import java.util.* import kotlin.math.* fun PrintWriter.solve() { val w = nextInt() val h = nextInt() val uf = UnionFind(w + 10) var components = w.toLong() var valid = true for (_i in 0 until h) { val q = nextLine() if (!valid) { println(0) continue } val lsts = Array(26) { mutableListOf<Int>() } for (i in 0 until w) { if (q[i] in 'a'..'z') { lsts[q[i] - 'a'].add(i) } else if (q[i] in '0'..'9') { val num = q[i] - '0' val j = w + num if (!uf.same(i, j)) { uf.unite(i, j) components-- } } } for (lst in lsts) { for (i in lst.indices) { if (!uf.same(lst[i], lst[0])) { uf.unite(lst[i], lst[0]) components-- } } } for (i in w..w + 9) { for (j in w until i) { if (uf.same(i, j)) { valid = false } } } println(if (valid) ModInt(10).pow(components) else ModInt(0)) } } 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) = find(x) == find(y) fun size(x: Int) = -parent[find(x)] } // region ModInt class ModInt(x: Long) { companion object { const val MOD = 998244353L //const val MOD = 1000000007L } constructor(y: Int) : this(y.toLong()) val x = (x % MOD + MOD) % MOD operator fun plus(other: ModInt): ModInt { return ModInt(x + other.x) } operator fun minus(other: ModInt): ModInt { return ModInt(x - other.x) } operator fun times(other: ModInt): ModInt { return ModInt(x * other.x) } operator fun div(other: ModInt): ModInt { return this * other.inv() } fun pow(exp: Long): ModInt { if (exp == 0L) return ModInt(1L) var a = pow(exp shr 1) a *= a if (exp and 1L == 0L) return a return this * a } fun inv(): ModInt { return this.pow(MOD - 2) } override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as ModInt if (x != other.x) return false return true } override fun hashCode(): Int { return x.hashCode() } override fun toString(): String { return "$x" } } class ModIntArray(n: Int) { private val a = LongArray(n) { 0L } constructor(n: Int, init: (Int) -> ModInt) : this(n) { for (i in 0 until n) { a[i] = init(i).x } } operator fun set(i: Int, x: ModInt) { a[i] = x.x } operator fun get(i: Int): ModInt { return ModInt(a[i]) } fun joinToString(s: String = ", "): String { return a.joinToString(s) } } fun Long.toModInt() = ModInt(this) fun Int.toModInt() = ModInt(this) fun binomSimple(n: Long, k: Long): ModInt { var numer = ModInt(1) var denom = ModInt(1) for (i in 0 until k) { numer *= ModInt(n - i) denom *= ModInt(k - i) } return numer / denom } // endregion fun main() { Thread(null, { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() }, "solve", abs(1L.shl(26))) .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } } .apply { start() }.join() } // 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