結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー tktk_snsn
提出日時 2020-06-06 13:43:55
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-12-23 12:22:00
合計ジャッジ時間 3,563 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数
        self.rank = [0] * (n + 1)

    def find(self, x):  # xの根となる要素番号を返す
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def getNodeLen(self, x):
        return -self.root[self.find(x)]


if __name__ == "__main__":
    N = int(input())
    uf = UF_tree(N)
    edge = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        edge[a] += 1
        edge[b] += 1
        uf.unite(a, b)

    island = set([uf.find(x) for x in range(N)])
    if len(island) == 1:
        print("Bob")
    elif len(island) >= 3:
        print("Alice")
    else:
        c = Counter(edge)
        if c[2] == N - 1:  # 1つ輪っかと1つの島だと連接にできる
            print("Bob")
        else:
            print("Alice")
0