結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー irumo8202irumo8202
提出日時 2022-01-31 08:34:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,402 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 78,976 KB
最終ジャッジ日時 2024-06-11 08:42:54
合計ジャッジ時間 5,579 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,272 KB
testcase_01 AC 49 ms
54,016 KB
testcase_02 AC 50 ms
53,888 KB
testcase_03 AC 49 ms
53,632 KB
testcase_04 AC 50 ms
53,888 KB
testcase_05 AC 50 ms
54,016 KB
testcase_06 AC 48 ms
54,016 KB
testcase_07 AC 54 ms
54,016 KB
testcase_08 AC 51 ms
54,144 KB
testcase_09 AC 51 ms
54,656 KB
testcase_10 WA -
testcase_11 AC 53 ms
54,272 KB
testcase_12 AC 52 ms
54,656 KB
testcase_13 AC 156 ms
77,184 KB
testcase_14 AC 150 ms
77,440 KB
testcase_15 AC 160 ms
77,056 KB
testcase_16 AC 157 ms
77,312 KB
testcase_17 AC 165 ms
77,184 KB
testcase_18 AC 183 ms
77,184 KB
testcase_19 WA -
testcase_20 AC 249 ms
77,952 KB
testcase_21 AC 280 ms
78,336 KB
testcase_22 AC 316 ms
78,592 KB
testcase_23 AC 290 ms
78,848 KB
testcase_24 AC 255 ms
78,080 KB
testcase_25 AC 315 ms
78,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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

        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 len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members


N = int(input())

uf = UnionFind(N)

cnt = N

for _ in range(N - 1):
    s, t = map(int, input().split())
    if uf.same(s, t):
        continue
    cnt -= 1
    uf.union(s, t)

print("Alice" if cnt >= 2 else "Bob")
0