結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー AzumabashiAzumabashi
提出日時 2020-04-27 10:37:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 1,908 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 32,256 KB
最終ジャッジ日時 2024-05-01 05:11:39
合計ジャッジ時間 5,323 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
11,008 KB
testcase_01 AC 29 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 29 ms
10,880 KB
testcase_05 AC 29 ms
11,008 KB
testcase_06 AC 32 ms
11,008 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 30 ms
10,880 KB
testcase_09 AC 30 ms
10,880 KB
testcase_10 AC 30 ms
10,880 KB
testcase_11 AC 31 ms
10,880 KB
testcase_12 AC 29 ms
10,880 KB
testcase_13 AC 73 ms
12,928 KB
testcase_14 AC 69 ms
12,672 KB
testcase_15 AC 67 ms
12,672 KB
testcase_16 AC 72 ms
12,928 KB
testcase_17 AC 74 ms
12,928 KB
testcase_18 AC 167 ms
17,536 KB
testcase_19 AC 178 ms
17,280 KB
testcase_20 AC 240 ms
19,456 KB
testcase_21 AC 360 ms
24,064 KB
testcase_22 AC 478 ms
27,392 KB
testcase_23 AC 477 ms
28,288 KB
testcase_24 AC 537 ms
32,256 KB
testcase_25 AC 480 ms
28,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, node):
        self.parent = [-1 for _ in range(node)]
        self.node = node

    def find(self, target):
        if self.parent[target] < 0:
            return target
        else:
            self.parent[target] = self.find(self.parent[target])
            return self.parent[target]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x == root_y:
            return
        if self.parent[root_x] > self.parent[root_y]:
            root_x, root_y = root_y, root_x
        self.parent[root_x] += self.parent[root_y]
        self.parent[root_y] = root_x

    def get_size(self, x):
        return -self.parent[self.find(x)]

    def get_roots(self):
        return [i for i, val in enumerate(self.parent) if val < 0]

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.node) if self.find(i) == root]

    def get_group_members(self):
        return {root: self.members(root) for root in self.get_roots()}


def main():
    islands = int(input())
    uf = UnionFind(islands)
    graph = [[] for _ in range(islands)]
    for _ in range(islands - 1):
        u, v = map(int, input().split())
        uf.union(u, v)
        graph[u].append(v)
        graph[v].append(u)
    can_bob_win = False
    all_roots = uf.get_roots()
    if len(all_roots) == 1:
        can_bob_win = True
    elif len(all_roots) == 2:
        group_member = uf.get_group_members()
        ok = 0
        for root in all_roots:
            if len(group_member[root]) < 3:
                ok += 1
            elif all(len(graph[v]) > 1 for v in group_member[root]):
                ok += 1
        if ok == 2:
            can_bob_win = True
    print("Bob" if can_bob_win else "Alice")


if __name__ == '__main__':
    main()

0