結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー norioc
提出日時 2024-03-20 17:14:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 1,355 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 80,360 KB
最終ジャッジ日時 2024-09-30 06:07:45
合計ジャッジ時間 3,762 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n: int):
        self.data = [-1] * (n+1)
        self.nexts = [i for i in range(n+1)]

    def root(self, a: int) -> int:
        if self.data[a] < 0: return a
        self.data[a] = self.root(self.data[a])
        return self.data[a]

    def unite(self, a: int, b: int) -> bool:
        pa = self.root(a)
        pb = self.root(b)
        if pa == pb: return False
        if self.data[pa] > self.data[pb]:
            pa, pb = pb, pa
        self.data[pa] += self.data[pb] # pa を pb をつなげる
        self.data[pb] = pa
        self.nexts[pa], self.nexts[pb] = self.nexts[pb], self.nexts[pa]
        return True

    def issame(self, a: int, b: int) -> bool:
        return self.root(a) == self.root(b)

    def size(self, a: int) -> int:
        """a が属する集合のサイズ"""
        return -self.data[self.root(a)]

    def group(self, a: int):
        """a が属する集合"""
        yield a
        x = a
        while self.nexts[x] != a:
            x = self.nexts[x]
            yield x


N = int(input())

uf = UnionFind(N)
degs = [0] * N
for _ in range(N-1):
    u, v = map(int, input().split())
    degs[u] += 1
    degs[v] += 1
    uf.unite(u, v)

s = set(uf.root(i) for i in range(N))
if uf.size(0) == N or len(s) <= 2 and 1 not in degs:
    print('Bob')
else:
    print('Alice')
0