結果

問題 No.1640 簡単な色塗り
ユーザー yakamotoyakamoto
提出日時 2021-08-08 15:20:24
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,163 ms / 2,000 ms
コード長 5,530 bytes
コンパイル時間 17,169 ms
コンパイル使用メモリ 443,756 KB
実行使用メモリ 129,808 KB
最終ジャッジ日時 2023-09-12 04:04:47
合計ジャッジ時間 59,012 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 262 ms
54,616 KB
testcase_01 AC 264 ms
54,684 KB
testcase_02 AC 267 ms
54,560 KB
testcase_03 AC 264 ms
54,824 KB
testcase_04 AC 802 ms
77,220 KB
testcase_05 AC 942 ms
129,808 KB
testcase_06 AC 257 ms
54,520 KB
testcase_07 AC 287 ms
54,540 KB
testcase_08 AC 257 ms
52,676 KB
testcase_09 AC 257 ms
52,688 KB
testcase_10 AC 746 ms
69,860 KB
testcase_11 AC 631 ms
66,104 KB
testcase_12 AC 624 ms
66,112 KB
testcase_13 AC 975 ms
77,668 KB
testcase_14 AC 901 ms
78,660 KB
testcase_15 AC 569 ms
64,412 KB
testcase_16 AC 599 ms
62,504 KB
testcase_17 AC 816 ms
76,504 KB
testcase_18 AC 424 ms
57,800 KB
testcase_19 AC 620 ms
65,168 KB
testcase_20 AC 685 ms
70,640 KB
testcase_21 AC 652 ms
66,744 KB
testcase_22 AC 414 ms
57,484 KB
testcase_23 AC 766 ms
69,880 KB
testcase_24 AC 471 ms
58,044 KB
testcase_25 AC 605 ms
66,532 KB
testcase_26 AC 797 ms
75,688 KB
testcase_27 AC 545 ms
62,352 KB
testcase_28 AC 892 ms
77,648 KB
testcase_29 AC 763 ms
73,780 KB
testcase_30 AC 407 ms
59,304 KB
testcase_31 AC 677 ms
78,612 KB
testcase_32 AC 678 ms
74,556 KB
testcase_33 AC 518 ms
68,360 KB
testcase_34 AC 603 ms
64,068 KB
testcase_35 AC 503 ms
68,052 KB
testcase_36 AC 433 ms
59,276 KB
testcase_37 AC 435 ms
59,596 KB
testcase_38 AC 605 ms
70,288 KB
testcase_39 AC 475 ms
60,688 KB
testcase_40 AC 478 ms
62,116 KB
testcase_41 AC 754 ms
86,084 KB
testcase_42 AC 487 ms
62,456 KB
testcase_43 AC 499 ms
61,148 KB
testcase_44 AC 501 ms
61,708 KB
testcase_45 AC 482 ms
64,064 KB
testcase_46 AC 427 ms
58,392 KB
testcase_47 AC 388 ms
59,200 KB
testcase_48 AC 610 ms
72,124 KB
testcase_49 AC 370 ms
54,712 KB
testcase_50 AC 261 ms
52,584 KB
testcase_51 AC 258 ms
52,672 KB
testcase_52 AC 1,163 ms
106,680 KB
testcase_53 AC 1,160 ms
111,948 KB
07_evil_01.txt AC 1,431 ms
107,412 KB
07_evil_02.txt AC 1,687 ms
136,108 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()}
          ^

ソースコード

diff #

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()
0