結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー tktk_snsntktk_snsn
提出日時 2020-06-06 13:43:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 13,040 KB
最終ジャッジ日時 2023-08-25 00:41:22
合計ジャッジ時間 3,064 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,616 KB
testcase_01 AC 18 ms
8,632 KB
testcase_02 AC 18 ms
8,776 KB
testcase_03 AC 18 ms
8,708 KB
testcase_04 AC 18 ms
8,620 KB
testcase_05 AC 18 ms
8,636 KB
testcase_06 AC 18 ms
8,652 KB
testcase_07 AC 19 ms
8,612 KB
testcase_08 AC 19 ms
8,660 KB
testcase_09 AC 19 ms
8,704 KB
testcase_10 AC 18 ms
8,620 KB
testcase_11 AC 19 ms
8,772 KB
testcase_12 AC 18 ms
8,756 KB
testcase_13 AC 40 ms
8,792 KB
testcase_14 AC 38 ms
9,064 KB
testcase_15 AC 38 ms
8,992 KB
testcase_16 AC 39 ms
8,800 KB
testcase_17 AC 39 ms
9,012 KB
testcase_18 AC 81 ms
10,060 KB
testcase_19 AC 83 ms
10,172 KB
testcase_20 AC 119 ms
10,780 KB
testcase_21 AC 176 ms
12,148 KB
testcase_22 AC 225 ms
13,040 KB
testcase_23 AC 235 ms
12,880 KB
testcase_24 AC 237 ms
12,940 KB
testcase_25 AC 225 ms
12,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数
        self.rank = [0] * (n + 1)

    def find(self, x):  # xの根となる要素番号を返す
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def getNodeLen(self, x):
        return -self.root[self.find(x)]


if __name__ == "__main__":
    N = int(input())
    uf = UF_tree(N)
    edge = [0] * N
    for _ in range(N-1):
        a, b = map(int, input().split())
        edge[a] += 1
        edge[b] += 1
        uf.unite(a, b)

    island = set([uf.find(x) for x in range(N)])
    if len(island) == 1:
        print("Bob")
    elif len(island) >= 3:
        print("Alice")
    else:
        c = Counter(edge)
        if c[2] == N - 1:  # 1つ輪っかと1つの島だと連接にできる
            print("Bob")
        else:
            print("Alice")
0