結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
terasa
|
| 提出日時 | 2022-05-27 00:18:26 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 236 ms / 2,000 ms |
| コード長 | 1,986 bytes |
| コンパイル時間 | 272 ms |
| コンパイル使用メモリ | 82,440 KB |
| 実行使用メモリ | 91,132 KB |
| 最終ジャッジ日時 | 2024-09-20 15:09:45 |
| 合計ジャッジ時間 | 3,715 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict, Counter
import bisect
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')
class UnionFind:
def __init__(self, N):
self.N = N
self.par = [-1] * N
def find(self, x):
if self.par[x] < 0:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
if x > y:
x, y = y, x
self.par[x] += self.par[y]
self.par[y] = x
return True
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -self.par[self.find(x)]
def roots(self):
return [i for i in range(self.N) if self.par[i] < 0]
def members(self, x):
p = self.find(x)
return [i for i in range(self.N) if self.find(i) == p]
def index_lt(a, x):
'return largest index s.t. A[i] < x or -1 if it does not exist'
return bisect.bisect_left(a, x) - 1
def index_le(a, x):
'return largest index s.t. A[i] <= x or -1 if it does not exist'
return bisect.bisect_right(a, x) - 1
def index_gt(a, x):
'return smallest index s.t. A[i] > x or len(a) if it does not exist'
return bisect.bisect_right(a, x)
def index_ge(a, x):
'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
return bisect.bisect_left(a, x)
N = int(input())
uf = UnionFind(N)
D = defaultdict(int)
for _ in range(N - 1):
u, v = map(int, input().split())
uf.unite(u, v)
D[u] += 1
D[v] += 1
roots = uf.roots()
if len(roots) == 1:
print('Bob')
elif len(roots) == 2:
for d in D.values():
if not d in {0, 2}:
print('Alice')
exit()
print('Bob')
else:
print('Alice')
terasa