結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー yakamotoyakamoto
提出日時 2023-02-03 22:39:49
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 795 ms / 2,000 ms
コード長 4,207 bytes
コンパイル時間 18,680 ms
コンパイル使用メモリ 441,092 KB
実行使用メモリ 71,608 KB
最終ジャッジ日時 2023-09-15 18:49:56
合計ジャッジ時間 44,321 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 276 ms
53,612 KB
testcase_01 AC 278 ms
53,916 KB
testcase_02 AC 278 ms
53,888 KB
testcase_03 AC 277 ms
54,048 KB
testcase_04 AC 280 ms
53,716 KB
testcase_05 AC 277 ms
53,684 KB
testcase_06 AC 277 ms
53,716 KB
testcase_07 AC 277 ms
54,020 KB
testcase_08 AC 279 ms
53,696 KB
testcase_09 AC 275 ms
53,684 KB
testcase_10 AC 276 ms
53,576 KB
testcase_11 AC 738 ms
69,384 KB
testcase_12 AC 744 ms
69,492 KB
testcase_13 AC 730 ms
69,560 KB
testcase_14 AC 718 ms
71,328 KB
testcase_15 AC 716 ms
67,364 KB
testcase_16 AC 656 ms
63,540 KB
testcase_17 AC 649 ms
63,252 KB
testcase_18 AC 276 ms
53,592 KB
testcase_19 AC 735 ms
69,308 KB
testcase_20 AC 732 ms
69,356 KB
testcase_21 AC 739 ms
69,492 KB
testcase_22 AC 793 ms
70,208 KB
testcase_23 AC 743 ms
69,512 KB
testcase_24 AC 741 ms
69,604 KB
testcase_25 AC 732 ms
67,160 KB
testcase_26 AC 723 ms
67,260 KB
testcase_27 AC 741 ms
69,568 KB
testcase_28 AC 795 ms
70,216 KB
testcase_29 AC 735 ms
71,608 KB
testcase_30 AC 738 ms
67,152 KB
testcase_31 AC 738 ms
69,676 KB
testcase_32 AC 715 ms
67,712 KB
testcase_33 AC 731 ms
67,496 KB
testcase_34 AC 731 ms
69,468 KB
testcase_35 AC 715 ms
67,308 KB
testcase_36 AC 723 ms
67,404 KB
testcase_37 AC 736 ms
69,452 KB
testcase_38 AC 701 ms
67,324 KB
testcase_39 AC 726 ms
69,360 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:159: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:163: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:167: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:171: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:173: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:180: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:187: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:204: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
//val MOD = 998244353

fun packUGraph(n: Int, from: IntArray, to: IntArray): Array<IntArray> {
  val p = IntArray(n)
  val m = from.size
  for (i in 0 until m) {
    ++p[from[i]]
    ++p[to[i]]
  }
  val g = Array(n){IntArray(p[it])}
  for (i in 0 until m) {
    g[from[i]][--p[from[i]]] = to[i]
    g[to[i]][--p[to[i]]] = from[i]
  }
  return g
}

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  fun solve() {
    val N = ni()
    val (from, to) = na2(N - 1, -1)
    val C = na(N)
    val g = packUGraph(N, from, to)
    val queue = ArrayDeque<Int>()
    val deg = IntArray(N)
    for (v in 0 until N) {
      deg[v] = g[v].size
      if (deg[v] == 1) queue += v
    }
    val deleted = BooleanArray(N)
    var cnt = 0

    fun del(v: Int) {
      deg[v]--
      if (deg[v] == 0) deleted[v] = true
      else if (deg[v] == 1) queue += v
    }

    while(queue.isNotEmpty()) {
      val v = queue.poll()!!
      if (deg[v] == 0) continue
      for (u in g[v]) {
        if (deleted[u]) continue
        del(v)
        del(u)
        assert(deg[v] == 0)
        if (C[v] == 0) {
          C[v] = C[v] xor 1
          C[u] = C[u] xor 1
          cnt++
        }
      }
    }
    out.println(if (C.contains(0)) -1 else cnt)
  }













































  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(FileOutputStream("./out.txt"))
      val out = java.io.PrintWriter(System.out)
      Solver(System.`in`, out).solve()
      out.flush()
    }
  }
}

/**
 * judgeから呼ばれる
 */
fun main() = Solver.main()
0