結果

問題 No.1817 Reversed Edges
ユーザー 👑 箱
提出日時 2022-01-21 22:23:30
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,279 ms / 2,000 ms
コード長 1,630 bytes
コンパイル時間 15,548 ms
コンパイル使用メモリ 425,832 KB
実行使用メモリ 84,060 KB
最終ジャッジ日時 2023-08-17 06:36:46
合計ジャッジ時間 41,366 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 342 ms
58,996 KB
testcase_01 AC 337 ms
58,860 KB
testcase_02 AC 337 ms
57,220 KB
testcase_03 AC 313 ms
57,056 KB
testcase_04 AC 312 ms
57,200 KB
testcase_05 AC 321 ms
57,124 KB
testcase_06 AC 318 ms
56,964 KB
testcase_07 AC 871 ms
70,464 KB
testcase_08 AC 694 ms
64,112 KB
testcase_09 AC 895 ms
70,864 KB
testcase_10 AC 712 ms
68,324 KB
testcase_11 AC 799 ms
70,184 KB
testcase_12 AC 1,215 ms
83,140 KB
testcase_13 AC 1,212 ms
84,048 KB
testcase_14 AC 1,226 ms
82,324 KB
testcase_15 AC 1,228 ms
82,248 KB
testcase_16 AC 1,269 ms
84,012 KB
testcase_17 AC 1,229 ms
83,984 KB
testcase_18 AC 1,279 ms
82,092 KB
testcase_19 AC 1,247 ms
84,060 KB
testcase_20 AC 1,234 ms
82,228 KB
testcase_21 AC 1,202 ms
82,688 KB
testcase_22 AC 923 ms
82,808 KB
testcase_23 AC 929 ms
83,332 KB
testcase_24 AC 906 ms
81,664 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