結果

問題 No.1640 簡単な色塗り
ユーザー 👑 箱星箱星
提出日時 2021-08-06 22:51:41
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 2,111 bytes
コンパイル時間 17,756 ms
コンパイル使用メモリ 425,788 KB
実行使用メモリ 430,168 KB
最終ジャッジ日時 2023-09-12 02:52:55
合計ジャッジ時間 25,334 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 307 ms
53,516 KB
testcase_01 AC 286 ms
50,564 KB
testcase_02 AC 307 ms
53,516 KB
testcase_03 AC 310 ms
53,112 KB
testcase_04 AC 1,452 ms
115,056 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val m = n
    val adj = Array(n) { mutableListOf<Int>() }
    val edges = mutableMapOf<Pair<Int, Int>, Stack<Int>>()
    for (i in 0 until m) {
        val v1 = nextInt() - 1
        val v2 = nextInt() - 1
        adj[v1].add(v2)
        adj[v2].add(v1)
        val pair = min(v1, v2) to max(v1, v2)
        if (!edges.containsKey(pair)) {
            edges[pair] = Stack()
        }
        edges[pair]?.push(i)
    }
    val choice = Array(n) { -1 }
    val visited = BooleanArray(n) { false }
    val chosen = BooleanArray(n) { false }
    for (i in 0 until n) {
        if (visited[i]) continue
        val stack = Stack<Pair<Int, Int>>()
        stack.push(i to -1)
        while (stack.isNotEmpty()) {
            val (v, p) = stack.pop()
            for (w in adj[v]) {
                if (w != p && w != v) {
                    stack.push(w to v)
                }
                val min = min(v, w)
                val max = max(v, w)
                if (edges[min to max]?.isNotEmpty()!!) {
                    val num = edges[min to max]?.pop()!!
                    if (!chosen[min]) {
                        chosen[min] = true
                        choice[num] = min
                    } else {
                        chosen[max] = true
                        choice[num] = max
                    }
                }
            }
        }
    }
    if (chosen.any { !it }) {
        println("No")
    } else {
        println("Yes")
        println(choice.map { it + 1 }.joinToString("\n"))
    }
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// 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
0