結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー 👑 rin204rin204
提出日時 2022-01-18 22:37:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,574 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 77,952 KB
最終ジャッジ日時 2024-05-02 19:34:20
合計ジャッジ時間 4,909 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
51,968 KB
testcase_01 AC 44 ms
51,840 KB
testcase_02 AC 44 ms
52,224 KB
testcase_03 AC 44 ms
52,608 KB
testcase_04 AC 43 ms
51,968 KB
testcase_05 WA -
testcase_06 AC 43 ms
52,608 KB
testcase_07 AC 47 ms
52,096 KB
testcase_08 WA -
testcase_09 AC 44 ms
52,864 KB
testcase_10 AC 44 ms
52,736 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 132 ms
76,288 KB
testcase_14 AC 133 ms
76,800 KB
testcase_15 AC 136 ms
76,672 KB
testcase_16 AC 131 ms
76,416 KB
testcase_17 WA -
testcase_18 AC 154 ms
76,672 KB
testcase_19 AC 182 ms
76,672 KB
testcase_20 AC 211 ms
77,568 KB
testcase_21 AC 237 ms
77,568 KB
testcase_22 AC 252 ms
77,824 KB
testcase_23 AC 249 ms
77,696 KB
testcase_24 WA -
testcase_25 AC 244 ms
77,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n = int(input())
UF = UnionFind(n)
for _ in range(n - 1):
    u, v = map(int, input().split())
    UF.union(u - 1, v - 1)
    
if UF.group_count() == 1:
    print("Bob")
    exit()

cnt = 0
for r in UF.roots():
    if UF.size(r) >= 2:
        cnt += 1
if cnt == 1:
    print("Bob")
else:
    print("Alice")

    
0