結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー tobusakanatobusakana
提出日時 2022-11-27 15:38:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 1,581 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 89,420 KB
最終ジャッジ日時 2024-04-14 23:21:38
合計ジャッジ時間 4,170 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,572 KB
testcase_01 AC 36 ms
53,728 KB
testcase_02 AC 37 ms
52,444 KB
testcase_03 AC 38 ms
52,672 KB
testcase_04 AC 37 ms
52,408 KB
testcase_05 AC 37 ms
53,700 KB
testcase_06 AC 37 ms
54,128 KB
testcase_07 AC 38 ms
53,000 KB
testcase_08 AC 38 ms
53,096 KB
testcase_09 AC 38 ms
53,136 KB
testcase_10 AC 40 ms
53,544 KB
testcase_11 AC 38 ms
54,372 KB
testcase_12 AC 38 ms
53,408 KB
testcase_13 AC 95 ms
77,484 KB
testcase_14 AC 93 ms
77,396 KB
testcase_15 AC 97 ms
77,620 KB
testcase_16 AC 93 ms
77,748 KB
testcase_17 AC 95 ms
77,772 KB
testcase_18 AC 113 ms
79,036 KB
testcase_19 AC 118 ms
79,012 KB
testcase_20 AC 156 ms
82,412 KB
testcase_21 AC 199 ms
85,924 KB
testcase_22 AC 240 ms
88,448 KB
testcase_23 AC 242 ms
89,052 KB
testcase_24 AC 236 ms
89,056 KB
testcase_25 AC 245 ms
89,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Bobに渡した時点で連結成分が2つであればBobの勝ち
# 従い、初期状態で以下のいずれかを満たせればAliceの勝ちである
# ・連結成分が1つ->Bobの勝ち
# ・連結成分が3つ以上ある->Aliceの勝ち
# ・連結成分が2つ
#   ->両方とも辺が複数あれば、必ずどちらかは木なのでAliceの勝ち
#   ->片方が孤立した頂点の場合、もう一方が完全な輪であれば(入り次数が全て2であれば)Bobの勝ち、そうでなければAliceの勝ち
#     言い換えると、入り次数が2の頂点がN - 1個、入り次数が0の頂点が1個だとBobの勝ち

N = int(input())
G = [[] for i in range(N)]
indegree = [0] * N
for _ in range(N - 1):
    u,v = map(int,input().split())
    G[u].append(v)
    G[v].append(u)
    indegree[u] += 1
    indegree[v] += 1
    
# 連結成分の数を数える
visited = [False] * N
cnt = 0
for start in range(N):
    if visited[start]:
        continue
    cnt += 1
    stack = [start]
    visited[start] = True
    while stack:
        v = stack.pop()
        for child in G[v]:
            if visited[child]:
                continue
            visited[child] = True
            stack.append(child)
            
if cnt == 1:
    print("Bob")
elif cnt >= 3:
    print("Alice")
else:
    ins_0 = 0
    ins_2 = 0
    for i in range(N):
        if indegree[i] == 0:
            ins_0 += 1
        elif indegree[i] == 2:
            ins_2 += 1
    if ins_0 == 1 and ins_2 == N - 1:
        print("Bob")
    else:
        print("Alice")
    
    
0