結果

問題 No.1094 木登り / Climbing tree
ユーザー 👑 箱
提出日時 2021-08-12 15:03:10
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 5,741 bytes
コンパイル時間 15,623 ms
コンパイル使用メモリ 471,248 KB
実行使用メモリ 226,608 KB
最終ジャッジ日時 2024-04-09 19:38:27
合計ジャッジ時間 66,073 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 278 ms
58,668 KB
testcase_01 TLE -
testcase_02 AC 1,516 ms
226,608 KB
testcase_03 AC 723 ms
94,164 KB
testcase_04 AC 1,156 ms
128,248 KB
testcase_05 AC 1,514 ms
176,684 KB
testcase_06 AC 1,359 ms
127,648 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,229 ms
123,996 KB
testcase_16 AC 1,814 ms
206,244 KB
testcase_17 AC 1,545 ms
159,556 KB
testcase_18 AC 1,387 ms
150,284 KB
testcase_19 AC 1,761 ms
190,656 KB
testcase_20 TLE -
testcase_21 AC 1,522 ms
170,860 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,994 ms
209,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

data class Edge(val node: Int, val cost: Long)

fun PrintWriter.solve() {
    val n = nextInt()
    val adj = Array(n) { mutableListOf<Int>() }
    val adj2 = Array(n) { mutableListOf<Edge>() }
    for (i in 0 until n - 1) {
        val v1 = nextInt() - 1
        val v2 = nextInt() - 1
        val c = nextLong()
        adj[v1].add(v2)
        adj[v2].add(v1)
        adj2[v1].add(Edge(v2, c))
        adj2[v2].add(Edge(v1, c))
    }
    val lca = LCA(adj, 0)
    val depth = LongArray(n) { 0L }
    val stack = Stack<Triple<Int, Int, Long>>()
    stack.push(Triple(0, -1, 0))
    while (stack.isNotEmpty()) {
        val (v, p, c) = stack.pop()
        if (p != -1) {
            depth[v] = depth[p] + c
        }
        for ((w, d) in adj2[v]) {
            if (w != p) {
                stack.push(Triple(w, v, d))
            }
        }
    }
    val q = nextInt()
    for (i in 0 until q) {
        val s = nextInt() - 1
        val t = nextInt() - 1
        println(depth[s] + depth[t] - 2 * depth[lca.lca(s, t)])
    }
}

class SegTree<Monoid>(private val n: Int, private val op: (Monoid, Monoid) -> Monoid, private val e: Monoid) {
    private var d: MutableList<Monoid>
    private var size = 1
    private var log = 0

    init {
        while (size < n) {
            size *= 2
            log++
        }
        d = MutableList(2 * size) { e }
    }

    constructor(a: Array<Monoid>, op: (Monoid, Monoid) -> Monoid, e: Monoid) : this(a.count(), op, e) {
        for (i in 0 until n) {
            d[size + i] = a[i]
        }
        for (i in size - 1 downTo 1) update(i)
    }

    operator fun set(p: Int, x: Monoid) {
        if (p !in 0 until n) {
            throw IllegalArgumentException()
        }
        val p1 = p + size
        d[p1] = x
        for (i in 1..log) {
            update(p1 shr i)
        }
    }

    operator fun get(p: Int): Monoid {
        if (p !in 0 until n) {
            throw IllegalArgumentException()
        }
        return d[p + size]
    }

    fun prod(range: IntRange): Monoid {
        val l = range.first
        val r = range.last + 1
        if (l !in 0..r || r > n) {
            throw IllegalArgumentException()
        }
        var sml = e
        var smr = e
        var l1 = l + size
        var r1 = r + size
        while (l1 < r1) {
            if (l1 and 1 != 0) sml = op(sml, d[l1++])
            if (r1 and 1 != 0) smr = op(d[--r1], smr)
            l1 /= 2
            r1 /= 2
        }
        return op(sml, smr)
    }

    fun allProd(): Monoid {
        return d[1]
    }

    fun maxRight(l: Int, f: (Monoid) -> Boolean): Int {
        if (l !in 0..n || !f(e)) {
            throw IllegalArgumentException()
        }
        if (l == n) return n
        var l1 = l + size
        var sm = e
        do {
            while (l1 % 2 == 0) l1 /= 2
            if (!f(op(sm, d[l1]))) {
                while (l1 < size) {
                    l1 *= 2
                    if (f(op(sm, d[l1]))) {
                        sm = op(sm, d[l1])
                        l1++
                    }
                }
                return l1 - size
            }
            sm = op(sm, d[l1])
            l1++
        } while ((l1 and -l1) != l1)
        return n
    }

    fun minLeft(r: Int, f: (Monoid) -> Boolean): Int {
        if (r !in 0..n || !f(e)) {
            throw IllegalArgumentException()
        }
        if (r == 0) return 0
        var r1 = r + size
        var sm = e
        do {
            r1--
            while (r1 > 1 && r1 % 2 != 0) r1 /= 2
            if (!f(op(d[r1], sm))) {
                while (r1 < size) {
                    r1 = 2 * r1 + 1
                    if (f(op(d[r1], sm))) {
                        sm = op(d[r1], sm)
                        r1--
                    }
                }
                return r1 + 1 - size
            }
            sm = op(d[r1], sm)
        } while ((r1 and -r1) != r1)
        return 0
    }

    private fun update(k: Int) {
        d[k] = op(d[2 * k], d[2 * k + 1])
    }

    fun joinToString(s: String = ", "): String {
        return (0 until n).map { this[it] }.joinToString(s)
    }
}

class LCA(private val adj: Array<MutableList<Int>>, private val root: Int) {
    private val n = adj.size
    private val depth = Array(n) { 0 }
    private val seg: SegTree<Pair<Int, Int>>
    private val tin = Array(n) { 0 }

    init {
        val lst = mutableListOf<Pair<Int, Int>>()
        fun dfs(v: Int, par: Int, d: Int) {
            tin[v] = lst.size
            lst.add(d to v)
            depth[v] = d
            for (w in adj[v]) {
                if (w != par) {
                    dfs(w, v, d + 1)
                }
                lst.add(d to v)
            }
        }
        dfs(root, -1, 0)
        seg = SegTree(lst.toTypedArray(), { x, y -> if (x.first < y.first) x else y }, Int.MAX_VALUE to -1)
    }

    fun lca(x: Int, y: Int): Int {
        val l = min(tin[x], tin[y])
        val r = max(tin[x], tin[y])
        return seg.prod(l..r).second
    }

    fun distance(x: Int, y: Int): Int {
        return depth[x] + depth[y] - 2 * depth[lca(x, y)]
    }
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", 1.shl(26)).start()
}

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