結果

問題 No.1424 Ultrapalindrome
ユーザー 箱星
提出日時 2020-09-18 01:17:41
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 2,599 bytes
コンパイル時間 17,154 ms
コンパイル使用メモリ 460,708 KB
実行使用メモリ 137,316 KB
最終ジャッジ日時 2024-11-24 07:47:38
合計ジャッジ時間 44,935 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 WA * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:28:25: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Int
    val max = dist.max()!!
                        ^
Main.kt:40:26: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Int
    val diam = dist.max()!!
                         ^
Main.kt:58:14: warning: variable 'min' is never used
    val (v1, min) = leaves.minBy { it.second }!!
             ^
Main.kt:58:47: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Pair<Int, Int>
    val (v1, min) = leaves.minBy { it.second }!!
                                              ^
Main.kt:75:43: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Int
    val l = leaves.map { it.second }.min()!!
                                          ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val adj = Array(n) { mutableSetOf<Int>() }
    for (i in 0 until n - 1) {
        val v1 = sc.nextInt() - 1
        val v2 = sc.nextInt() - 1
        adj[v1].add(v2)
        adj[v2].add(v1)
    }
    val que: Queue<Triple<Int, Int, Int>> = ArrayDeque()
    que.add(Triple(0, -1, 0))
    val dist = Array(n) { 0 }
    while (que.count() > 0) {
        val (v, p, d) = que.poll()
        dist[v] = d
        for (w in adj[v]) {
            if (w != p) {
                que.add(Triple(w, v, d + 1))
            }
        }
    }
    val max = dist.max()!!
    val v0 = (0 until n).first { dist[it] == max }
    que.add(Triple(v0, -1, 0))
    while (que.count() > 0) {
        val (v, p, d) = que.poll()
        dist[v] = d
        for (w in adj[v]) {
            if (w != p) {
                que.add(Triple(w, v, d + 1))
            }
        }
    }
    val diam = dist.max()!!

    que.add(Triple(0, -1, 0))
    val leaves = mutableListOf<Pair<Int, Int>>()
    while (que.count() > 0) {
        val (v, p, d) = que.poll()
        dist[v] = d
        var isleaf = true
        for (w in adj[v]) {
            if (w != p) {
                isleaf = false
                que.add(Triple(w, v, d + 1))
            }
        }
        if (isleaf) {
            leaves.add(v to d)
        }
    }
    val (v1, min) = leaves.minBy { it.second }!!
    que.add(Triple(v1, -1, 0))
    leaves.clear()
    while (que.count() > 0) {
        val (v, p, d) = que.poll()
        dist[v] = d
        var isleaf = true
        for (w in adj[v]) {
            if (w != p) {
                isleaf = false
                que.add(Triple(w, v, d + 1))
            }
        }
        if (isleaf) {
            leaves.add(v to d)
        }
    }
    val l = leaves.map { it.second }.min()!!
    println(if (diam == l) "Yes" else "No")
}

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

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    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()
    fun ready() = br.ready()
}
0