結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー noriocnorioc
提出日時 2024-03-20 17:06:02
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,235 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 78,188 KB
最終ジャッジ日時 2024-03-20 17:06:06
合計ジャッジ時間 4,784 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 37 ms
53,460 KB
testcase_03 AC 38 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 38 ms
53,460 KB
testcase_08 AC 38 ms
53,460 KB
testcase_09 AC 37 ms
53,460 KB
testcase_10 WA -
testcase_11 AC 60 ms
53,460 KB
testcase_12 AC 37 ms
53,460 KB
testcase_13 AC 120 ms
76,440 KB
testcase_14 AC 117 ms
76,312 KB
testcase_15 AC 115 ms
76,312 KB
testcase_16 AC 115 ms
76,312 KB
testcase_17 AC 120 ms
76,448 KB
testcase_18 AC 142 ms
76,796 KB
testcase_19 WA -
testcase_20 AC 222 ms
77,320 KB
testcase_21 AC 221 ms
78,124 KB
testcase_22 AC 249 ms
78,188 KB
testcase_23 AC 237 ms
78,132 KB
testcase_24 AC 210 ms
78,008 KB
testcase_25 AC 261 ms
78,132 KB
権限があれば一括ダウンロードができます

ソースコード

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)
for _ in range(N-1):
    u, v = map(int, input().split())
    uf.unite(u, v)

if uf.size(0) == N:
    print('Bob')
else:
    print('Alice')
0