結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー 👑 tamatotamato
提出日時 2020-01-31 21:55:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 1,596 bytes
コンパイル時間 1,021 ms
コンパイル使用メモリ 81,832 KB
実行使用メモリ 89,248 KB
最終ジャッジ日時 2023-10-17 09:35:19
合計ジャッジ時間 4,810 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,368 KB
testcase_01 AC 38 ms
53,368 KB
testcase_02 AC 37 ms
53,368 KB
testcase_03 AC 37 ms
53,368 KB
testcase_04 AC 37 ms
53,368 KB
testcase_05 AC 37 ms
53,368 KB
testcase_06 AC 38 ms
53,368 KB
testcase_07 AC 38 ms
53,368 KB
testcase_08 AC 38 ms
53,368 KB
testcase_09 AC 38 ms
53,368 KB
testcase_10 AC 39 ms
53,368 KB
testcase_11 AC 38 ms
53,368 KB
testcase_12 AC 38 ms
53,368 KB
testcase_13 AC 115 ms
76,880 KB
testcase_14 AC 122 ms
77,400 KB
testcase_15 AC 122 ms
77,336 KB
testcase_16 AC 111 ms
76,876 KB
testcase_17 AC 118 ms
77,208 KB
testcase_18 AC 156 ms
79,624 KB
testcase_19 AC 185 ms
80,744 KB
testcase_20 AC 220 ms
82,628 KB
testcase_21 AC 281 ms
86,392 KB
testcase_22 AC 330 ms
89,248 KB
testcase_23 AC 306 ms
88,952 KB
testcase_24 AC 250 ms
88,536 KB
testcase_25 AC 312 ms
88,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            if self.root[x] < 0:
                return x
            else:
                self.root[x] = self.find_root(self.root[x])
                return self.root[x]

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    N = int(input())
    UF = UnionFind(N+1)
    adj = [[] for _ in range(N)]
    for _ in range(N-1):
        u, v = map(int, input().split())
        UF.unite(u, v)
        adj[u].append(v)
        adj[v].append(u)
    s = UF.size(0)
    if s == N:
        print('Bob')
    elif s == N-1 or s == 1:
        flg = 0
        for v in range(N):
            if len(adj[v]) == 1:
                flg += 1
        if flg:
            print('Alice')
        else:
            print('Bob')
    else:
        print('Alice')


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