結果
問題 | No.1640 簡単な色塗り |
ユーザー | yakamoto |
提出日時 | 2021-08-08 15:20:24 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,215 ms / 2,000 ms |
コード長 | 5,530 bytes |
コンパイル時間 | 16,987 ms |
コンパイル使用メモリ | 467,640 KB |
実行使用メモリ | 137,952 KB |
最終ジャッジ日時 | 2024-06-29 17:04:34 |
合計ジャッジ時間 | 60,466 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 284 ms
50,136 KB |
testcase_01 | AC | 279 ms
50,072 KB |
testcase_02 | AC | 281 ms
50,180 KB |
testcase_03 | AC | 282 ms
49,936 KB |
testcase_04 | AC | 830 ms
93,724 KB |
testcase_05 | AC | 836 ms
137,952 KB |
testcase_06 | AC | 284 ms
50,204 KB |
testcase_07 | AC | 281 ms
50,152 KB |
testcase_08 | AC | 288 ms
50,156 KB |
testcase_09 | AC | 285 ms
50,132 KB |
testcase_10 | AC | 748 ms
95,632 KB |
testcase_11 | AC | 690 ms
83,420 KB |
testcase_12 | AC | 638 ms
78,192 KB |
testcase_13 | AC | 940 ms
104,588 KB |
testcase_14 | AC | 942 ms
98,644 KB |
testcase_15 | AC | 590 ms
71,184 KB |
testcase_16 | AC | 615 ms
75,240 KB |
testcase_17 | AC | 899 ms
105,520 KB |
testcase_18 | AC | 446 ms
56,552 KB |
testcase_19 | AC | 655 ms
77,420 KB |
testcase_20 | AC | 739 ms
94,728 KB |
testcase_21 | AC | 658 ms
80,544 KB |
testcase_22 | AC | 430 ms
55,264 KB |
testcase_23 | AC | 808 ms
100,860 KB |
testcase_24 | AC | 517 ms
63,028 KB |
testcase_25 | AC | 655 ms
78,248 KB |
testcase_26 | AC | 848 ms
101,248 KB |
testcase_27 | AC | 593 ms
70,360 KB |
testcase_28 | AC | 973 ms
104,540 KB |
testcase_29 | AC | 846 ms
95,972 KB |
testcase_30 | AC | 419 ms
57,684 KB |
testcase_31 | AC | 724 ms
98,676 KB |
testcase_32 | AC | 695 ms
98,936 KB |
testcase_33 | AC | 533 ms
74,052 KB |
testcase_34 | AC | 618 ms
82,592 KB |
testcase_35 | AC | 518 ms
73,704 KB |
testcase_36 | AC | 455 ms
60,904 KB |
testcase_37 | AC | 452 ms
60,616 KB |
testcase_38 | AC | 664 ms
94,084 KB |
testcase_39 | AC | 480 ms
66,648 KB |
testcase_40 | AC | 490 ms
67,728 KB |
testcase_41 | AC | 760 ms
111,324 KB |
testcase_42 | AC | 497 ms
69,056 KB |
testcase_43 | AC | 509 ms
70,692 KB |
testcase_44 | AC | 527 ms
71,644 KB |
testcase_45 | AC | 487 ms
66,084 KB |
testcase_46 | AC | 463 ms
59,140 KB |
testcase_47 | AC | 419 ms
56,240 KB |
testcase_48 | AC | 672 ms
94,040 KB |
testcase_49 | AC | 391 ms
54,104 KB |
testcase_50 | AC | 281 ms
50,132 KB |
testcase_51 | AC | 283 ms
50,028 KB |
testcase_52 | AC | 1,215 ms
107,240 KB |
testcase_53 | AC | 1,115 ms
117,996 KB |
07_evil_01.txt | AC | 1,512 ms
126,920 KB |
07_evil_02.txt | AC | 1,698 ms
144,316 KB |
コンパイルメッセージ
Main.kt:227:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun debug(a: LongArray) { ^ Main.kt:231:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun debug(a: IntArray) { ^ Main.kt:235:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun debug(a: BooleanArray) { ^ Main.kt:239:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")} ^ Main.kt:241:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun debugDim(A: Array<LongArray>) { ^ Main.kt:248:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun debugDim(A: Array<IntArray>) { ^ Main.kt:255:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun debugDim(A: Array<BooleanArray>) { ^ Main.kt:272:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()} ^
ソースコード
import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.lang.AssertionError import java.util.* import kotlin.math.abs import kotlin.math.max import kotlin.math.min val MOD = 1_000_000_007 class UnionFind(n: Int) { private val par = IntArray(n){it} private val rank = IntArray(n){1} // 集合の要素数 private val visits = IntArray(n) // 訪れた場所をfind毎に用意するのがもったいないのでつかいまわす fun find(x: Int): Int { var ptr = 0 var i = x while(par[i] != i) { visits[ptr++] = i i = par[i] } for (j in 0 until ptr) { par[visits[j]] = i } return i } private fun merge(node: Int, rt: Int): Int { par[node] = rt rank[rt] += rank[node] return rt } fun unite(x: Int, y: Int): Boolean { val x1 = find(x) val y1 = find(y) return if (x1 == y1) false else { if (rank[x1] < rank[y1]) merge(x1, y1) else merge(y1, x1) true } } fun isSame(x: Int, y: Int) = find(x) == find(y) fun isRoot(x: Int) = par[x] == x /** * xを解決する必要がないときは直にrankをみる */ fun cntNodes(x: Int): Int = rank[find(x)] fun inspect() = run{par} } data class Edge(val id: Int, val u: Int) class Solver(stream: InputStream, private val out: java.io.PrintWriter) { private val reader = BufferedReader(InputStreamReader(stream), 32768) fun solve() { val th = Thread(null, Runnable { if (!solve2()) { out.println("No") } },"solve", 1 shl 26) th.start() th.join() } fun solve2(): Boolean { val N = ni() val (from, to) = na2(N, -1) val g = Array(N){ mutableListOf<Edge>()} val uf = UnionFind(N) for (i in 0 until N) { g[from[i]].add(Edge(i, to[i])) g[to[i]].add(Edge(i, from[i])) uf.unite(from[i], to[i]) } val edgeCnt = IntArray(N) for (i in 0 until N) { edgeCnt[uf.find(from[i])]++ } val ans = IntArray(N){-1} fun dfs2(v: Int, eId: Int) { ans[eId] = v debug{"dfs2($v $eId)"} for (e in g[v]) { if (ans[e.id] != -1) continue dfs2(e.u, e.id) } } val path = mutableSetOf<Int>() val visited = BooleanArray(N) // edgeを使ったか /** * @return cycleが見つかった */ fun dfs(v: Int) { path += v debug{"dfs($v) $path"} for (e in g[v]) { if (visited[e.id]) continue visited[e.id] = true if (path.contains(e.u)) { debug{"start dfs2 $v ${e.id}"} dfs2(v, e.id) } else { dfs(e.u) } } path -= v } for (v in 0 until N) { if (uf.isRoot(v)) { if (edgeCnt[v] != uf.cntNodes(v)) return false path.clear() dfs(v) } } out.println("Yes") ans.map{it+1}.forEach(out::println) return true } private val isDebug = try { // なんか本番でエラーでる System.getenv("MY_DEBUG") != null } catch (t: Throwable) { false } private var tokenizer: StringTokenizer? = null private fun next(): String { while (tokenizer == null || !tokenizer!!.hasMoreTokens()) { tokenizer = StringTokenizer(reader.readLine()) } return tokenizer!!.nextToken() } private fun ni() = next().toInt() private fun nl() = next().toLong() private fun ns() = next() private fun na(n: Int, offset: Int = 0): IntArray { return IntArray(n) { ni() + offset } } private fun nal(n: Int, offset: Int = 0): LongArray { val res = LongArray(n) for (i in 0 until n) { res[i] = nl() + offset } return res } private fun na2(n: Int, offset: Int = 0): Array<IntArray> { val a = Array(2){IntArray(n)} for (i in 0 until n) { for (e in a) { e[i] = ni() + offset } } return a } private inline fun debug(msg: () -> String) { if (isDebug) System.err.println(msg()) } /** * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと */ private inline fun debug(a: LongArray) { debug { a.joinToString(" ") } } private inline fun debug(a: IntArray) { debug { a.joinToString(" ") } } private inline fun debug(a: BooleanArray) { debug { toString(a) } } private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")} private inline fun debugDim(A: Array<LongArray>) { if (isDebug) { for (a in A) { debug(a) } } } private inline fun debugDim(A: Array<IntArray>) { if (isDebug) { for (a in A) { debug(a) } } } private inline fun debugDim(A: Array<BooleanArray>) { if (isDebug) { for (a in A) { debug(a) } } } /** * 勝手にimport消されるのを防ぎたい */ private fun hoge() { min(1, 2) max(1, 2) abs(-10) } private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()} private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())} companion object { // TestRunnerから呼びたいので単純なmainじゃだめ fun main() { val out = java.io.PrintWriter(System.out) Solver(System.`in`, out).solve() out.flush() } } } /** * judgeから呼ばれる */ fun main() = Solver.main()