結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー irumo8202irumo8202
提出日時 2022-01-31 09:13:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 1,481 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 11,068 KB
実行使用メモリ 11,464 KB
最終ジャッジ日時 2023-09-02 02:04:45
合計ジャッジ時間 4,889 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,632 KB
testcase_01 AC 17 ms
8,676 KB
testcase_02 AC 17 ms
8,804 KB
testcase_03 AC 17 ms
8,780 KB
testcase_04 AC 18 ms
8,748 KB
testcase_05 AC 18 ms
8,620 KB
testcase_06 AC 17 ms
8,768 KB
testcase_07 AC 18 ms
8,764 KB
testcase_08 AC 18 ms
8,608 KB
testcase_09 AC 18 ms
8,676 KB
testcase_10 AC 18 ms
8,604 KB
testcase_11 AC 18 ms
8,628 KB
testcase_12 AC 18 ms
8,680 KB
testcase_13 AC 53 ms
8,820 KB
testcase_14 AC 52 ms
8,904 KB
testcase_15 AC 53 ms
8,956 KB
testcase_16 AC 55 ms
8,952 KB
testcase_17 AC 55 ms
9,000 KB
testcase_18 AC 132 ms
9,348 KB
testcase_19 AC 135 ms
9,520 KB
testcase_20 AC 195 ms
10,208 KB
testcase_21 AC 306 ms
10,896 KB
testcase_22 AC 385 ms
11,464 KB
testcase_23 AC 391 ms
11,200 KB
testcase_24 AC 399 ms
11,160 KB
testcase_25 AC 404 ms
11,104 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

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

if 1 in edges:
    cnt += 1
print("Alice" if cnt != 2 else "Bob")
0