結果

問題 No.1817 Reversed Edges
ユーザー 👑 箱星箱星
提出日時 2022-01-21 22:23:30
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,135 ms / 2,000 ms
コード長 1,630 bytes
コンパイル時間 15,224 ms
コンパイル使用メモリ 444,536 KB
実行使用メモリ 114,952 KB
最終ジャッジ日時 2024-05-04 13:31:14
合計ジャッジ時間 34,665 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
54,448 KB
testcase_01 AC 299 ms
54,332 KB
testcase_02 AC 297 ms
54,376 KB
testcase_03 AC 302 ms
54,456 KB
testcase_04 AC 287 ms
54,388 KB
testcase_05 AC 295 ms
54,604 KB
testcase_06 AC 313 ms
54,268 KB
testcase_07 AC 848 ms
95,560 KB
testcase_08 AC 648 ms
84,540 KB
testcase_09 AC 822 ms
95,656 KB
testcase_10 AC 629 ms
86,636 KB
testcase_11 AC 715 ms
93,384 KB
testcase_12 AC 1,102 ms
113,932 KB
testcase_13 AC 1,116 ms
109,688 KB
testcase_14 AC 1,135 ms
106,496 KB
testcase_15 AC 1,091 ms
114,624 KB
testcase_16 AC 1,087 ms
109,604 KB
testcase_17 AC 1,082 ms
106,556 KB
testcase_18 AC 1,098 ms
114,640 KB
testcase_19 AC 1,084 ms
114,488 KB
testcase_20 AC 1,114 ms
114,952 KB
testcase_21 AC 1,097 ms
109,568 KB
testcase_22 AC 870 ms
104,264 KB
testcase_23 AC 862 ms
104,576 KB
testcase_24 AC 858 ms
105,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val adj = Array(n) { mutableListOf<Int>() }
    for (i in 0 until n - 1) {
        val v1 = nextInt() - 1
        val v2 = nextInt() - 1
        adj[v1].add(v2)
        adj[v2].add(v1)
    }
    val ans = Array(n) { 0 }
    val stack = Stack<Pair<Int, Int>>()
    stack.push(0 to -1)
    while (stack.isNotEmpty()) {
        val (v, p) = stack.pop()
        for (w in adj[v]) {
            if (w != p) {
                stack.push(w to v)
                if (w < v) ans[0]++
            }
        }
    }
    stack.push(0 to -1)
    while (stack.isNotEmpty()) {
        val (v, p) = stack.pop()
        if (p != -1) {
            ans[v] = ans[p] + if (v > p) 1 else -1
        }
        for (w in adj[v]) {
            if (w != p) {
                stack.push(w to v)
            }
        }
    }
    println(ans.joinToString("\n"))
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

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