結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー neterukunneterukun
提出日時 2020-02-01 21:09:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 1,797 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 81,728 KB
実行使用メモリ 99,252 KB
最終ジャッジ日時 2023-10-19 00:21:17
合計ジャッジ時間 4,475 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,580 KB
testcase_01 AC 38 ms
53,580 KB
testcase_02 AC 38 ms
53,580 KB
testcase_03 AC 39 ms
53,580 KB
testcase_04 AC 39 ms
53,580 KB
testcase_05 AC 38 ms
53,580 KB
testcase_06 AC 38 ms
53,580 KB
testcase_07 AC 39 ms
53,580 KB
testcase_08 AC 40 ms
53,580 KB
testcase_09 AC 39 ms
53,580 KB
testcase_10 AC 39 ms
53,580 KB
testcase_11 AC 39 ms
53,580 KB
testcase_12 AC 39 ms
53,580 KB
testcase_13 AC 129 ms
78,668 KB
testcase_14 AC 130 ms
78,716 KB
testcase_15 AC 132 ms
78,668 KB
testcase_16 AC 127 ms
78,668 KB
testcase_17 AC 141 ms
78,776 KB
testcase_18 AC 170 ms
83,196 KB
testcase_19 AC 193 ms
83,644 KB
testcase_20 AC 228 ms
87,748 KB
testcase_21 AC 264 ms
94,004 KB
testcase_22 AC 306 ms
98,160 KB
testcase_23 AC 289 ms
99,252 KB
testcase_24 AC 264 ms
99,248 KB
testcase_25 AC 294 ms
99,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = [-1] * n
        self.cnt = n
        self.n = n

    def root(self, x):
        """頂点xの根を求める"""
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]

    def merge(self, x, y):
        """頂点xを含む集合と頂点y含む集合を結合する"""
        x = self.root(x)
        y = self.root(y)
        if x != y:
            if self.parent[x] > self.parent[y]:
                x, y = y, x
            self.parent[x] += self.parent[y]
            self.parent[y] = x
            self.cnt -= 1

    def is_same(self, x, y):
        """頂点xと頂点yが同じ集合に属するかどうかを返す"""
        return self.root(x) == self.root(y) 
    
    def get_size(self, x):
        """頂点xを含む集合の要素数を返す"""
        return -self.parent[self.root(x)]
    
    def get_cnt(self):
        """木の個数を返す"""
        return self.cnt

    def members(self, x):
        """頂点xが属する集合の要素を列挙する
        計算量に注意"""
        root_x = self.root(x)
        return [i for i in range(self.n) if self.root(i) == root_x]


n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]

tree = [[] for i in range(n)]
for i in range(n - 1):
    a, b = info[i]
    tree[a].append(b)
    tree[b].append(a)


uf = UnionFind(n)
for i in range(n - 1):
    a, b = info[i]
    uf.merge(a, b)

if uf.get_cnt() == 1:
    print("Bob")
elif uf.get_cnt() == 2:
    for i in range(n):
        if not(len(tree[i]) == 2 or len(tree[i]) == 0):
            print("Alice")
            break
    else:
        print("Bob")
else:
    print("Alice")
0