結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-06-09 14:38:38 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
AC
|
| 実行時間 | 398 ms / 5,000 ms |
| コード長 | 4,538 bytes |
| コンパイル時間 | 20,531 ms |
| コンパイル使用メモリ | 497,296 KB |
| 実行使用メモリ | 59,344 KB |
| 最終ジャッジ日時 | 2025-06-09 14:39:07 |
| 合計ジャッジ時間 | 24,734 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
import java.io.BufferedOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.PrintWriter
import java.util.*
import kotlin.math.*
import java.math.BigInteger
val singleTest: Boolean = true
fun solve() {
val (w, h) = nextIntArray(2)
val grid = Array(h){nextIntArray(w)}
val graph = Array(h*w){mutableListOf<Int>()}
fun id(i: Int, j: Int): Int { return i*w+j; }
for(i in 0 until h-1){
for(j in 0 until w){
if(grid[i][j]==grid[i+1][j]){
graph[id(i,j)].add(id(i+1,j))
graph[id(i+1,j)].add(id(i,j))
}
}
}
for(i in 0 until h){
for(j in 0 until w-1){
if(grid[i][j]==grid[i][j+1]){
graph[id(i,j)].add(id(i,j+1))
graph[id(i,j+1)].add(id(i,j))
}
}
}
val vis = BooleanArray(h*w){false}
var found = false
for(i in 0 until h*w){
if(vis[i]) continue
vis[i]=true
var E=0
var V=1
val stack = ArrayDeque<Int>()
stack.add(i)
while(!stack.isEmpty()){
val u = stack.pollLast()
E+=graph[u].size
for(v in graph[u]){
if(vis[v]) continue
vis[v]=true
V+=1
stack.add(v)
}
}
E/=2
if(E>=V){
found=true
break
}
}
println(if(found) "possible" else "impossible")
}
fun main() {
val numOfTests = if (singleTest) 1 else ni()
for (test in 1.. numOfTests) solve()
out.flush()
}
// reader and writer template
class FastScanner(private val stream: InputStream) {
private val buf = ByteArray(1024)
private var curChar = 0
private var numChars = 0
private fun read(): Int {
if (numChars == -1) throw Exception("Input miss match!")
if (curChar >= numChars) {
curChar = 0
try {
numChars = stream.read(buf)
} catch (e: IOException) {
throw Exception("Input miss match!")
}
if (numChars <= 0) return -1
}
return buf[curChar++].toInt()
}
fun nextInt(): Int = nextLong().toInt()
fun nextLong(): Long {
var c = read()
while (isWhitespace(c)) c = read()
var negative = false
if (c == '-'.code) {
negative = true
c = read()
}
var res: Long = 0
do {
if (c < '0'.code || c > '9'.code) throw Exception("Input miss match!")
res = res * 10 + (c - '0'.code)
c = read()
} while (!isWhitespace(c))
return if (negative) -res else res
}
fun nextDouble(): Double = next().toDouble()
fun nextChar(): Char {
var c = read()
while (isWhitespace(c)) c = read()
return c.toChar()
}
fun next(): String {
var c = read()
while (isWhitespace(c)) c = read()
val res = StringBuilder()
do {
res.appendCodePoint(c)
c = read()
} while (!isWhitespace(c))
return res.toString()
}
private fun isWhitespace(c: Int): Boolean = c == ' '.code || c == '\n'.code || c == '\r'.code || c == '\t'.code || c == -1
}
val scanner = FastScanner(System.`in`)
var out = PrintWriter(BufferedOutputStream(System.`out`))
fun ni() = scanner.nextInt()
fun nl() = scanner.nextLong()
fun nd() = scanner.nextDouble()
fun nc() = scanner.nextChar()
fun ns() = scanner.next()
fun nextIntArray(n: Int) = Array(n) { ni() }
fun nextLongArray(n: Int) = Array(n) { nl() }
fun nextDoubleArray(n: Int) = Array(n) { nd() }
fun nextCharArray(n: Int) = Array(n) { scanner.nextChar() }
fun nextIntGrid(n: Int, m: Int) = Array(n) { nextIntArray(m) }
fun nextLongGrid(n: Int, m: Int) = Array(n) { nextLongArray(m) }
fun nextDoubleGrid(n: Int, m: Int) = Array(n) { nextDoubleArray(m) }
fun nextCharGrid(n: Int, m: Int) = Array(n) { nextCharArray(m)}
fun print(array: Array<Int>) = out.println(array.joinToString(" "))
fun print(array: Array<Long>) = out.println(array.joinToString(" "))
fun print(array: Array<Double>) = out.println(array.joinToString(" "))
fun print(array: IntArray) = out.println(array.joinToString(" "))
fun print(array: LongArray) = out.println(array.joinToString(" "))
fun print(array: DoubleArray) = out.println(array.joinToString(" "))
fun print(list: List<Any>) = out.println(list.joinToString(" "))
fun print(obj: Any) = out.println(obj.toString())