結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー kohei2019kohei2019
提出日時 2021-02-04 14:29:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 477 ms / 2,000 ms
コード長 1,722 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 91,604 KB
最終ジャッジ日時 2023-09-13 08:46:00
合計ジャッジ時間 7,146 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,816 KB
testcase_01 AC 96 ms
71,696 KB
testcase_02 AC 97 ms
71,660 KB
testcase_03 AC 96 ms
71,688 KB
testcase_04 AC 100 ms
71,660 KB
testcase_05 AC 97 ms
71,832 KB
testcase_06 AC 99 ms
71,584 KB
testcase_07 AC 99 ms
71,800 KB
testcase_08 AC 97 ms
71,580 KB
testcase_09 AC 101 ms
71,824 KB
testcase_10 AC 100 ms
71,564 KB
testcase_11 AC 100 ms
71,452 KB
testcase_12 AC 96 ms
71,920 KB
testcase_13 AC 188 ms
80,408 KB
testcase_14 AC 193 ms
80,676 KB
testcase_15 AC 193 ms
80,276 KB
testcase_16 AC 189 ms
79,840 KB
testcase_17 AC 214 ms
80,164 KB
testcase_18 AC 226 ms
82,236 KB
testcase_19 AC 307 ms
83,576 KB
testcase_20 AC 364 ms
86,116 KB
testcase_21 AC 423 ms
89,540 KB
testcase_22 AC 477 ms
91,576 KB
testcase_23 AC 463 ms
90,832 KB
testcase_24 AC 344 ms
91,604 KB
testcase_25 AC 437 ms
91,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#unionfind経路圧縮あり
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = list(range(n))

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if self.parents[x] > self.parents[y]:
            x, y = y, x
            
        if x == y:
            return

        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 == i]

    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
import collections
N = int(input())
#全連結すなわち木ならBob
#N-1の円環ループ+1頂点ならBob
#それ以外Alice
lsg = [[] for i in range(N)]
UF = UnionFind(N)
for i in range(N-1):
    u,v = map(int,input().split())
    lsg[u].append(v)
    lsg[v].append(u)
    UF.union(u,v)
if len(UF.roots()) == 1:
    print('Bob')
    sys.exit()
if len(UF.roots()) >= 3:
    print('Alice')
    sys.exit()
lsn = [0]*(N)
for i in range(N):
    lsn[i] = len(lsg[i])
if lsn.count(0) == 1 and lsn.count(2) == N-1:
    print('Bob')
else:
    print('Alice')
0