結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー convexineq
提出日時 2020-01-31 21:42:03
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 1,442 bytes
コンパイル時間 75 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-09-17 07:30:27
合計ジャッジ時間 2,972 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read


n = int(input())

res = [0]*n
UF = UnionFind(n)
cnt = n
for _ in range(n-1):
    a,b = [int(i) for i in readline().split()]
    res[a] += 1
    res[b] += 1
    if UF.merge(a,b): cnt -= 1

d = res.count(1)
#print(cnt,d)
if cnt >= 3:
    print("Alice")
elif cnt == 1:
    print("Bob")
else:
    if d == 0: 
        print("Bob")
    else:
        print("Alice")
        









0