結果

問題 No.1424 Ultrapalindrome
ユーザー 👑 箱
提出日時 2020-09-18 00:19:19
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 892 ms / 2,000 ms
コード長 2,741 bytes
コンパイル時間 15,309 ms
コンパイル使用メモリ 425,552 KB
実行使用メモリ 112,020 KB
最終ジャッジ日時 2023-08-04 18:50:54
合計ジャッジ時間 30,141 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 248 ms
50,652 KB
testcase_01 AC 249 ms
50,244 KB
testcase_02 AC 242 ms
50,256 KB
testcase_03 AC 242 ms
50,280 KB
testcase_04 AC 241 ms
50,632 KB
testcase_05 AC 244 ms
50,252 KB
testcase_06 AC 241 ms
50,456 KB
testcase_07 AC 244 ms
50,304 KB
testcase_08 AC 246 ms
50,288 KB
testcase_09 AC 565 ms
74,568 KB
testcase_10 AC 568 ms
74,648 KB
testcase_11 AC 487 ms
71,028 KB
testcase_12 AC 554 ms
74,668 KB
testcase_13 AC 399 ms
60,016 KB
testcase_14 AC 515 ms
70,444 KB
testcase_15 AC 260 ms
50,376 KB
testcase_16 AC 475 ms
68,480 KB
testcase_17 AC 511 ms
70,544 KB
testcase_18 AC 613 ms
70,288 KB
testcase_19 AC 609 ms
72,496 KB
testcase_20 AC 404 ms
60,300 KB
testcase_21 AC 547 ms
68,752 KB
testcase_22 AC 486 ms
64,448 KB
testcase_23 AC 372 ms
56,644 KB
testcase_24 AC 404 ms
61,528 KB
testcase_25 AC 410 ms
61,540 KB
testcase_26 AC 646 ms
76,784 KB
testcase_27 AC 892 ms
94,368 KB
testcase_28 AC 735 ms
92,760 KB
testcase_29 AC 844 ms
107,056 KB
testcase_30 AC 813 ms
107,748 KB
testcase_31 AC 842 ms
112,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class UnionFind(n: Int) {
    private val rank = IntArray(n) { 0 }
    private val parent = IntArray(n) { -1 }

    fun find(x: Int): Int {
        if (parent[x] < 0) {
            return x
        }
        parent[x] = find(parent[x])
        return parent[x]
    }

    fun unite(x: Int, y: Int) {
        val x1 = find(x)
        val y1 = find(y)
        if (x1 == y1) {
            return
        }
        if (rank[x1] < rank[y1]) {
            parent[y1] += parent[x1]
            parent[x1] = y1
        } else {
            parent[x1] += parent[y1]
            parent[y1] = x1
            if (rank[x1] == rank[y1]) {
                rank[x1]++
            }
        }
    }

    fun same(x: Int, y: Int): Boolean {
        return find(x) == find(y)
    }

    fun size(x: Int): Int {
        return -parent[find(x)]
    }
}

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val uf = UnionFind(n)
    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)
        uf.unite(v1, v2)
    }
    if (uf.size(0) != n) {
        throw Exception()
    }
    val lst = mutableListOf<Int>()
    for (i in 0 until n) {
        if (adj[i].count() >= 3) {
            lst.add(i)
        }
    }
    if (lst.count() >= 2) {
        println("No")
    } else if (lst.count() == 0) {
        println("Yes")
    } else {
        val lens = mutableSetOf<Int>()
        val que: Queue<Triple<Int, Int, Int>> = ArrayDeque()
        que.add(Triple(lst[0], -1, 0))
        while (que.count() > 0) {
            val (v, p, d) = que.poll()
            var isleaf = true
            for (w in adj[v]) {
                if (w != p) {
                    isleaf = false
                    que.add(Triple(w, v, d + 1))
                }
            }
            if (isleaf) {
                lens.add(d)
            }
        }
        println(if (lens.count() == 1) "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