結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー stngstng
提出日時 2022-08-16 21:42:26
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,549 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 82,500 KB
実行使用メモリ 93,268 KB
最終ジャッジ日時 2024-04-14 15:23:59
合計ジャッジ時間 5,955 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,736 KB
testcase_01 AC 43 ms
55,716 KB
testcase_02 AC 43 ms
54,680 KB
testcase_03 AC 43 ms
54,668 KB
testcase_04 AC 43 ms
55,576 KB
testcase_05 AC 43 ms
54,892 KB
testcase_06 AC 43 ms
54,820 KB
testcase_07 AC 44 ms
56,060 KB
testcase_08 AC 45 ms
56,268 KB
testcase_09 AC 44 ms
55,492 KB
testcase_10 WA -
testcase_11 AC 44 ms
55,796 KB
testcase_12 AC 44 ms
55,532 KB
testcase_13 AC 183 ms
79,588 KB
testcase_14 AC 177 ms
79,648 KB
testcase_15 AC 171 ms
79,864 KB
testcase_16 AC 182 ms
79,492 KB
testcase_17 AC 184 ms
79,604 KB
testcase_18 AC 246 ms
82,636 KB
testcase_19 AC 266 ms
82,836 KB
testcase_20 AC 320 ms
86,300 KB
testcase_21 AC 370 ms
89,812 KB
testcase_22 AC 445 ms
93,000 KB
testcase_23 AC 427 ms
91,220 KB
testcase_24 AC 425 ms
93,268 KB
testcase_25 AC 425 ms
91,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1 for _ in range(n+1)]
    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n = int(input())
uv = [[] for i in range(n)]
ki = UnionFind(n+1)
for i in range(n-1):
    u,v = map(int,input().split())
    uv[u].append(v)
    uv[v].append(u)
    f,s = u,v
    if not ki.same_check(u,v):
        ki.union(u,v)

f = True
idx = ki.find(0)
for i in range(n):
    if ki.find(i) != idx:
        f = False
if f == True:
    print("Bob")
    exit()

d = deque()
d.append(u)
li = [0]*n
li[f] = 1
li[u] = 1

while len(d) > 0:
    tmp = d.popleft()
    for i in range(len(uv[tmp])):
        if li[uv[tmp][i]] == 1:
            lst = uv[tmp][i]
            continue
        else:
            d.append(uv[tmp][i])
            li[uv[tmp][i]] = 1

if lst != f:
    print("Alice")
else:
    print("Bob")
0