結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー noriocnorioc
提出日時 2024-03-20 17:14:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 1,355 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 79,956 KB
最終ジャッジ日時 2024-03-20 17:14:28
合計ジャッジ時間 4,517 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 36 ms
53,456 KB
testcase_08 AC 38 ms
53,460 KB
testcase_09 AC 37 ms
53,460 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 36 ms
53,460 KB
testcase_12 AC 37 ms
53,460 KB
testcase_13 AC 130 ms
77,076 KB
testcase_14 AC 155 ms
76,956 KB
testcase_15 AC 126 ms
77,084 KB
testcase_16 AC 128 ms
77,076 KB
testcase_17 AC 133 ms
76,824 KB
testcase_18 AC 151 ms
77,156 KB
testcase_19 AC 180 ms
77,392 KB
testcase_20 AC 210 ms
78,460 KB
testcase_21 AC 268 ms
78,964 KB
testcase_22 AC 286 ms
79,956 KB
testcase_23 AC 246 ms
79,296 KB
testcase_24 AC 227 ms
79,040 KB
testcase_25 AC 244 ms
79,296 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)
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