結果

問題 No.1424 Ultrapalindrome
ユーザー 👑 箱星箱星
提出日時 2020-09-18 00:19:19
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 877 ms / 2,000 ms
コード長 2,741 bytes
コンパイル時間 16,878 ms
コンパイル使用メモリ 447,336 KB
実行使用メモリ 117,520 KB
最終ジャッジ日時 2024-04-22 16:51:29
合計ジャッジ時間 35,301 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 271 ms
50,112 KB
testcase_01 AC 276 ms
49,656 KB
testcase_02 AC 274 ms
49,756 KB
testcase_03 AC 282 ms
49,712 KB
testcase_04 AC 287 ms
49,992 KB
testcase_05 AC 285 ms
49,684 KB
testcase_06 AC 282 ms
49,924 KB
testcase_07 AC 285 ms
49,916 KB
testcase_08 AC 276 ms
49,808 KB
testcase_09 AC 645 ms
101,104 KB
testcase_10 AC 646 ms
101,432 KB
testcase_11 AC 531 ms
79,952 KB
testcase_12 AC 598 ms
101,116 KB
testcase_13 AC 457 ms
62,900 KB
testcase_14 AC 587 ms
98,884 KB
testcase_15 AC 288 ms
50,080 KB
testcase_16 AC 507 ms
76,028 KB
testcase_17 AC 539 ms
81,956 KB
testcase_18 AC 552 ms
76,204 KB
testcase_19 AC 685 ms
99,700 KB
testcase_20 AC 470 ms
61,672 KB
testcase_21 AC 637 ms
80,208 KB
testcase_22 AC 527 ms
70,612 KB
testcase_23 AC 410 ms
57,476 KB
testcase_24 AC 442 ms
61,000 KB
testcase_25 AC 437 ms
60,324 KB
testcase_26 AC 693 ms
101,192 KB
testcase_27 AC 843 ms
117,520 KB
testcase_28 AC 759 ms
117,044 KB
testcase_29 AC 843 ms
116,956 KB
testcase_30 AC 831 ms
114,792 KB
testcase_31 AC 877 ms
117,228 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