結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー buey_tbuey_t
提出日時 2023-03-27 10:50:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 463 ms / 2,000 ms
コード長 3,604 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 81,512 KB
実行使用メモリ 113,900 KB
最終ジャッジ日時 2023-10-19 14:01:08
合計ジャッジ時間 7,309 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
88,192 KB
testcase_01 AC 150 ms
88,188 KB
testcase_02 AC 143 ms
88,192 KB
testcase_03 AC 142 ms
88,196 KB
testcase_04 AC 145 ms
88,192 KB
testcase_05 AC 148 ms
88,192 KB
testcase_06 AC 140 ms
88,188 KB
testcase_07 AC 150 ms
88,192 KB
testcase_08 AC 146 ms
88,188 KB
testcase_09 AC 148 ms
88,192 KB
testcase_10 AC 146 ms
88,224 KB
testcase_11 AC 142 ms
88,196 KB
testcase_12 AC 147 ms
88,220 KB
testcase_13 AC 211 ms
90,348 KB
testcase_14 AC 202 ms
90,136 KB
testcase_15 AC 205 ms
90,148 KB
testcase_16 AC 206 ms
90,388 KB
testcase_17 AC 220 ms
97,936 KB
testcase_18 AC 262 ms
93,328 KB
testcase_19 AC 278 ms
113,900 KB
testcase_20 AC 297 ms
94,944 KB
testcase_21 AC 348 ms
98,304 KB
testcase_22 AC 413 ms
100,908 KB
testcase_23 AC 426 ms
101,632 KB
testcase_24 AC 463 ms
103,832 KB
testcase_25 AC 436 ms
101,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum
    from heapq import heapify, heappop, heappush
    from bisect import bisect_left, bisect_right
    from copy import deepcopy
    import copy
    import random
    from collections import deque,Counter,defaultdict
    from itertools import permutations,combinations
    from decimal import Decimal,ROUND_HALF_UP
    #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    from functools import lru_cache, reduce
    #@lru_cache(maxsize=None)
    from operator import add,sub,mul,xor,and_,or_,itemgetter
    INF = 10**18
    mod1 = 10**9+7
    mod2 = 998244353
    
    #DecimalならPython
    #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    
    '''
    
    
    
    '''
    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.parents = [-1] * n
            #親だけ
            self.edge = [0]*n
    
        def find(self, x):
            if self.parents[x] < 0:
                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
            
            self.edge[x] += 1
            
            if x == y:
                return
    
            self.parents[x] += self.parents[y]
            self.edge[x] += self.edge[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):
            group_members = defaultdict(list)
            for member in range(self.n):
                group_members[self.find(member)].append(member)
            return group_members
    
        def __str__(self):
            return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
    
    #サイクルになってて一つだけ孤立してるパターン
    
    try:
        import pypyjit
        pypyjit.set_param('max_unroll_recursion=-1')
    except:
        pass
    try:
        import sys
        sys.setrecursionlimit(10**7)
    except:
        pass
    
    N = int(input())
    
    uf = UnionFind(N)
    
    def dfs(pos,r,cnt):
        
        if seen[r] == 1 and pos == r:
            print('Bob')
            exit()
        seen[pos] = 1
        
        for nx in G[pos]:
            if seen[nx] == 0 or (nx == r and cnt == N-1):
                dfs(nx,r,cnt+1)
    
    G = [[] for _ in range(N)]
    for _ in range(N-1):
        u,v = map(int, input().split())
        
        uf.union(u,v)
        G[u].append(v)
        G[v].append(u)
    
    if uf.group_count() == 1:
        print('Bob')
    elif uf.group_count() == 2:
        for r in uf.roots():
            seen = [0]*N
            dfs(r,r,1)
        
        print('Alice')
    else:
        print('Alice')
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
if __name__ == '__main__':
    main()
0