結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,668 KB
testcase_01 AC 18 ms
8,624 KB
testcase_02 AC 18 ms
8,676 KB
testcase_03 AC 18 ms
8,684 KB
testcase_04 AC 19 ms
8,724 KB
testcase_05 AC 19 ms
8,716 KB
testcase_06 AC 19 ms
8,544 KB
testcase_07 AC 19 ms
8,604 KB
testcase_08 AC 18 ms
8,604 KB
testcase_09 AC 19 ms
8,716 KB
testcase_10 AC 19 ms
8,712 KB
testcase_11 AC 19 ms
8,600 KB
testcase_12 AC 20 ms
8,620 KB
testcase_13 AC 57 ms
8,832 KB
testcase_14 AC 55 ms
8,776 KB
testcase_15 AC 56 ms
8,820 KB
testcase_16 AC 58 ms
8,808 KB
testcase_17 AC 56 ms
8,960 KB
testcase_18 AC 134 ms
9,264 KB
testcase_19 AC 134 ms
9,628 KB
testcase_20 AC 211 ms
10,048 KB
testcase_21 AC 317 ms
10,908 KB
testcase_22 AC 404 ms
11,444 KB
testcase_23 AC 427 ms
11,088 KB
testcase_24 AC 412 ms
11,092 KB
testcase_25 AC 431 ms
11,168 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