結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー strangerxxxstrangerxxx
提出日時 2022-08-30 11:12:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,822 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 152,960 KB
最終ジャッジ日時 2024-04-24 21:21:52
合計ジャッジ時間 73,224 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,136 KB
testcase_01 AC 34 ms
11,136 KB
testcase_02 AC 32 ms
11,392 KB
testcase_03 AC 33 ms
11,136 KB
testcase_04 AC 34 ms
11,264 KB
testcase_05 AC 33 ms
11,136 KB
testcase_06 AC 34 ms
11,136 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 32 ms
11,136 KB
testcase_34 AC 1,704 ms
74,496 KB
testcase_35 AC 2,890 ms
152,960 KB
testcase_36 AC 2,400 ms
93,312 KB
testcase_37 AC 32 ms
11,136 KB
testcase_38 AC 1,358 ms
11,136 KB
testcase_39 AC 3,083 ms
152,960 KB
testcase_40 AC 2,886 ms
116,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def resolve():
    n, m, q = map(int, input().split())
    edges = [[] for _ in range(n)]
    for _ in range(m):
        u, v = map(lambda x: int(x) - 1, input().split())
        edges[u].append(v)
        edges[v].append(u)
    bridge, _ = bridge_detection(edges)
    u = UnionFind(n)
    for i, j in bridge:
            u.union(i, j)
    for _ in range(q):
        x, y = map(lambda x: int(x) - 1, input().split())
        if u.same(x, y):
            print("Yes")
        else:
            print("No")


def bridge_detection(graph, start=0):
    # 橋、二重辺連結成分
    import sys
    RECURSION_LIMIT = 10 ** 6
    sys.setrecursionlimit(RECURSION_LIMIT)
    n = len(graph)
    order = [-1] * n
    bridges = []
    cycle_graph = [set() for _ in range(n)]
    cnt = -1

    def dfs(u, prev):
        nonlocal cnt
        cnt += 1
        low_pt = order[u] = cnt
        for v in graph[u]:
            if v == prev:
                continue
            if order[v] == -1:
                v_low_pt = dfs(v, u)
                if v_low_pt > order[u]:
                    bridges.append(tuple(sorted([u, v])))
                else:
                    cycle_graph[u].add(v)
                    cycle_graph[v].add(u)
                low_pt = min(low_pt, v_low_pt)
            else:
                low_pt = min(low_pt, order[v])
                cycle_graph[u].add(v)
                cycle_graph[v].add(u)
        return low_pt
    dfs(start, -1)
    return sorted(bridges), cycle_graph


class UnionFind:
    def __init__(self, n: int) -> None:
        self.n = n
        self.parent = [-1] * n
        self.groups = n

    def find(self, x: int) -> int:
        if self.parent[x] < 0:
            return x
        else:
            p = x
            while self.parent[p] >= 0:
                p = self.parent[p]
            while self.parent[x] >= 0:
                self.parent[x], x = p, self.parent[x]
            return p

    def union(self, x: int, y: int) -> bool:
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        self.groups -= 1
        return True

    def union_right(self, x: int, y: int) -> bool:
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if y > x:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        self.groups -= 1
        return True

    def union_left(self, x: int, y: int) -> bool:
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if x > y:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        self.groups -= 1
        return True

    def size(self, x: int) -> int:
        return -self.parent[self.find(x)]

    def same(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)

    def members(self, x: int) -> list:
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self) -> list:
        return [i for i, x in enumerate(self.parent) if x < 0]

    def group_count(self) -> int:
        return self.groups

    def sizes(self) -> dict:
        return {i: -x for i, x in enumerate(self.parent) if x < 0}

    def all_group_members(self) -> dict:
        from collections import defaultdict
        d = defaultdict(list)
        for i in range(self.n):
            p = self.find(i)
            d[p].append(i)
        return d

    def __str__(self) -> str:
        return '\n'.join('{}: {}'.format(k, v)
                         for k, v in self.all_group_members().items())


if __name__ == '__main__':
    resolve()
0