結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー yuly3yuly3
提出日時 2020-05-01 17:31:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 1,890 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 89,944 KB
最終ジャッジ日時 2023-08-25 23:22:12
合計ジャッジ時間 5,483 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
71,468 KB
testcase_01 AC 63 ms
71,248 KB
testcase_02 AC 63 ms
71,524 KB
testcase_03 AC 63 ms
71,260 KB
testcase_04 AC 66 ms
71,148 KB
testcase_05 AC 61 ms
71,380 KB
testcase_06 AC 59 ms
71,172 KB
testcase_07 AC 59 ms
71,524 KB
testcase_08 AC 62 ms
71,152 KB
testcase_09 AC 64 ms
71,356 KB
testcase_10 AC 61 ms
71,428 KB
testcase_11 AC 62 ms
71,560 KB
testcase_12 AC 65 ms
71,196 KB
testcase_13 AC 126 ms
78,492 KB
testcase_14 AC 125 ms
78,480 KB
testcase_15 AC 128 ms
79,168 KB
testcase_16 AC 117 ms
78,572 KB
testcase_17 AC 131 ms
79,188 KB
testcase_18 AC 151 ms
81,596 KB
testcase_19 AC 170 ms
81,516 KB
testcase_20 AC 196 ms
83,920 KB
testcase_21 AC 230 ms
87,696 KB
testcase_22 AC 263 ms
89,672 KB
testcase_23 AC 247 ms
89,340 KB
testcase_24 AC 223 ms
89,164 KB
testcase_25 AC 256 ms
89,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


class UnionFind:
    def __init__(self, n: int):
        self.n = n
        self.parents = [-1] * n
    
    def find(self, x: int):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    def union(self, x: int, y: int):
        x = self.find(x)
        y = self.find(y)
        
        if x == y:
            return
        if self.parents[y] < self.parents[x]:
            x, y = y, x
        
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def size(self, x: int):
        return -self.parents[self.find(x)]
    
    def same(self, x: int, y: int):
        return self.find(x) == self.find(y)
    
    def members(self, x: int):
        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):
        return {r: self.members(r) for r in self.roots()}
    
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def solve():
    N = int(rl())
    uf = UnionFind(N)
    graph = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = map(int, rl().split())
        uf.union(u, v)
        graph[u].append(v)
        graph[v].append(u)
    
    g_cnt = uf.group_count()
    if g_cnt == 1:
        print('Bob')
    elif g_cnt == 2:
        bridge = False
        for children in graph:
            if len(children) == 1:
                bridge = True
                break
        print('Alice' if bridge else 'Bob')
    else:
        print('Alice')


if __name__ == '__main__':
    solve()
0