結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー Coki628Coki628
提出日時 2020-05-07 12:07:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 698 ms / 2,000 ms
コード長 4,254 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 11,616 KB
実行使用メモリ 52,164 KB
最終ジャッジ日時 2023-09-16 07:51:32
合計ジャッジ時間 5,095 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,936 KB
testcase_01 AC 18 ms
8,892 KB
testcase_02 AC 17 ms
8,832 KB
testcase_03 AC 18 ms
8,280 KB
testcase_04 AC 17 ms
8,832 KB
testcase_05 AC 17 ms
8,844 KB
testcase_06 AC 18 ms
8,840 KB
testcase_07 AC 18 ms
8,948 KB
testcase_08 AC 18 ms
9,000 KB
testcase_09 AC 18 ms
8,940 KB
testcase_10 AC 17 ms
8,860 KB
testcase_11 AC 18 ms
8,356 KB
testcase_12 AC 18 ms
8,856 KB
testcase_13 AC 68 ms
13,596 KB
testcase_14 AC 48 ms
11,532 KB
testcase_15 AC 48 ms
11,528 KB
testcase_16 AC 68 ms
13,364 KB
testcase_17 AC 62 ms
14,708 KB
testcase_18 AC 191 ms
21,896 KB
testcase_19 AC 184 ms
26,944 KB
testcase_20 AC 220 ms
22,240 KB
testcase_21 AC 366 ms
29,508 KB
testcase_22 AC 479 ms
35,108 KB
testcase_23 AC 505 ms
36,368 KB
testcase_24 AC 698 ms
52,164 KB
testcase_25 AC 478 ms
36,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
EPS = 10 ** -10

class UnionFind:
    """ Union-Find木 """

    def __init__(self, n):
        self.n = n
        self.par = [i for i in range(n)]
        self.rank = [0] * n
        self.size = [1] * n
        self.tree = [True] * n
        self.grpcnt = n

    def find(self, x):
        """ 根の検索(グループ番号の取得) """
        t = []
        while self.par[x] != x:
            t.append(x)
            x = self.par[x]
        for i in t:
            self.par[i] = x
        return self.par[x]

    def union(self, x, y):
        """ 併合 """
        x = self.find(x)
        y = self.find(y)

        if x == y:
            self.tree[x] = False
            return
        if not self.tree[x] or not self.tree[y]:
            self.tree[x] = self.tree[y] = False

        self.grpcnt -= 1
        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 is_same(self, x, y):
        """ 同じ集合に属するか判定 """
        return self.find(x) == self.find(y)

    def get_size(self, x=None):
        if x is not None:
            """ あるノードの属する集合のノード数 """
            return self.size[self.find(x)]
        else:
            """ 集合の数 """
            return self.grpcnt
    
    def is_tree(self, x):
        """ 木かどうかの判定 """
        return self.tree[self.find(x)]

def low_link(nodes):
    """ Low-Link(関節点aps(set)と橋bridges(list)を列挙する) """

    N = len(nodes)
    visited = [False] * N
    prenum = [0] * N
    parent = [0] * N
    lowest = [0] * N
    bridges = []
    timer = 1

    def rec(cur, prev, timer):
        # curを訪問した直後の処理
        prenum[cur] = lowest[cur] = timer
        timer += 1
        visited[cur] = True
        for nxt in nodes[cur]:
            if not visited[nxt]:
                # curからvへ訪問する直前の処理
                parent[nxt] = cur
                timer = rec(nxt, cur, timer)
                # nxtの探索が終了した直後の処理
                lowest[cur] = min(lowest[cur], lowest[nxt])
                # より近い経路を含まないなら橋とする
                if lowest[nxt] == prenum[nxt]:
                    # 番号の小さい方から入れる
                    bridges.append((min(cur, nxt), max(cur, nxt)))
            elif nxt != prev:
                # cur -> nxt がback-edgeの場合の処理
                lowest[cur] = min(lowest[cur], prenum[nxt])
        return timer
    # 必要な各値の取得(非連結に対応するため全頂点から)
    for i in range(N):
        if not visited[i]:
            timer = rec(i, -1, timer)

    # 間接点
    aps = set()
    # ルートの子ノードの数
    np = 0
    for i in range(1, N):
        p = parent[i]
        if p == 0:
            np += 1
        # 条件2の確認
        elif prenum[p] <= lowest[i]:
            aps.add(p)
    # 条件1の確認
    if np > 1:
        aps.add(0)

    return aps, bridges

N = INT()
edges = []
uf = UnionFind(N)
nodes = [[] for i in range(N)]
for i in range(N-1):
    a, b = MAP()
    nodes[a].append(b)
    nodes[b].append(a)
    edges.append((a, b))
    uf.union(a, b)

if uf.get_size() == 1:
    print('Bob')
elif uf.get_size() >= 3:
    print('Alice')
else:
    arts, bridges = low_link(nodes)
    if bridges:
        print('Alice')
    else:
        print('Bob')
0