結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー naniwazunaniwazu
提出日時 2020-09-16 22:22:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,728 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 79,472 KB
最終ジャッジ日時 2023-09-04 04:05:10
合計ジャッジ時間 4,211 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,204 KB
testcase_01 AC 73 ms
71,564 KB
testcase_02 AC 73 ms
71,568 KB
testcase_03 AC 76 ms
71,384 KB
testcase_04 AC 74 ms
71,392 KB
testcase_05 AC 73 ms
71,352 KB
testcase_06 AC 75 ms
71,152 KB
testcase_07 AC 74 ms
71,080 KB
testcase_08 AC 74 ms
71,292 KB
testcase_09 AC 72 ms
71,584 KB
testcase_10 AC 74 ms
71,344 KB
testcase_11 AC 75 ms
71,460 KB
testcase_12 AC 74 ms
71,288 KB
testcase_13 AC 121 ms
77,608 KB
testcase_14 AC 123 ms
77,924 KB
testcase_15 AC 122 ms
77,608 KB
testcase_16 AC 119 ms
77,604 KB
testcase_17 AC 123 ms
77,720 KB
testcase_18 AC 129 ms
78,456 KB
testcase_19 AC 127 ms
78,304 KB
testcase_20 AC 140 ms
78,376 KB
testcase_21 AC 150 ms
78,780 KB
testcase_22 AC 154 ms
78,904 KB
testcase_23 AC 156 ms
79,068 KB
testcase_24 AC 161 ms
79,472 KB
testcase_25 AC 161 ms
79,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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:
            st = []
            while self.parents[x] >= 0:
                st.append(x)
                x = self.parents[x]
            for y in st:
                self.parents[y] = x
            return 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):
        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())
import sys
input = sys.stdin.buffer.readline
N = int(input())
uf = UnionFind(N)
num = [0]*N
for i in range(N-1):
    u,v = map(int,input().split())
    uf.union(u,v)
    num[u] += 1
    num[v] += 1
if uf.group_count() == 1:
    print('Bob')
else:
    A = True
    if uf.group_count() == 2:
        A = False
        for i in range(N):
            if num[i] != 2 and num[i] != 0:
                A = True
    if A:
        print('Alice')
    else:
        print('Bob')
0