結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー 2000 Ekiben2000 Ekiben
提出日時 2024-06-20 17:33:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
実行時間 -
コード長 1,426 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 824,732 KB
最終ジャッジ日時 2024-06-20 17:33:28
合計ジャッジ時間 8,031 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 31 ms
10,880 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 31 ms
11,008 KB
testcase_04 AC 32 ms
10,880 KB
testcase_05 AC 32 ms
10,880 KB
testcase_06 AC 32 ms
10,880 KB
testcase_07 AC 210 ms
11,264 KB
testcase_08 AC 546 ms
11,392 KB
testcase_09 AC 390 ms
11,776 KB
testcase_10 AC 838 ms
11,904 KB
testcase_11 AC 551 ms
11,392 KB
testcase_12 AC 647 ms
12,672 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

def fnd(p, x):
    if p[x] != x:
        p[x] = fnd(p, p[x])
    return p[x]

def uni(p, r, x, y):
    rx, ry = fnd(p, x), fnd(p, y)
    if rx != ry:
        if r[rx] > r[ry]:
            p[ry] = rx
        elif r[rx] < r[ry]:
            p[rx] = ry
        else:
            p[ry] = rx
            r[rx] += 1

def conn(g, n):
    p = list(range(n))
    r = [0] * n
    for u in g:
        for v in g[u]:
            uni(p, r, u, v)
    root = fnd(p, 0)
    return all(fnd(p, i) == root for i in range(n))

def solve():
    n = int(input().strip())
    b = [tuple(map(int, input().strip().split())) for _ in range(n - 1)]
    
    g = defaultdict(set)
    for u, v in b:
        g[u].add(v)
        g[v].add(u)
    
    for u, v in b:
        g[u].remove(v)
        g[v].remove(u)
        
        if not conn(g, n):
            pairs = {(i, j) for i in range(n) for j in range(i + 1, n) if j not in g[i]}
            can_reconnect = False
            for x, y in pairs:
                g[x].add(y)
                g[y].add(x)
                if conn(g, n):
                    can_reconnect = True
                g[x].remove(y)
                g[y].remove(x)
                if can_reconnect:
                    break
            if not can_reconnect:
                print("Alice")
                return
        
        g[u].add(v)
        g[v].add(u)
    
    print("Bob")

solve()
0