結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー strangerxxxstrangerxxx
提出日時 2022-08-30 11:25:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,083 ms / 4,000 ms
コード長 4,115 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 383,324 KB
最終ジャッジ日時 2024-11-07 06:39:08
合計ジャッジ時間 57,208 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,784 KB
testcase_01 AC 46 ms
54,656 KB
testcase_02 AC 47 ms
54,912 KB
testcase_03 AC 48 ms
54,656 KB
testcase_04 AC 48 ms
54,656 KB
testcase_05 AC 48 ms
54,656 KB
testcase_06 AC 48 ms
54,912 KB
testcase_07 AC 48 ms
54,784 KB
testcase_08 AC 245 ms
80,768 KB
testcase_09 AC 275 ms
80,768 KB
testcase_10 AC 284 ms
83,828 KB
testcase_11 AC 319 ms
90,240 KB
testcase_12 AC 292 ms
80,904 KB
testcase_13 AC 1,424 ms
139,440 KB
testcase_14 AC 1,578 ms
171,096 KB
testcase_15 AC 1,448 ms
125,424 KB
testcase_16 AC 828 ms
118,832 KB
testcase_17 AC 1,675 ms
182,220 KB
testcase_18 AC 1,234 ms
120,372 KB
testcase_19 AC 2,179 ms
263,796 KB
testcase_20 AC 1,350 ms
122,312 KB
testcase_21 AC 1,716 ms
174,968 KB
testcase_22 AC 1,991 ms
215,076 KB
testcase_23 AC 3,048 ms
354,132 KB
testcase_24 AC 2,931 ms
308,652 KB
testcase_25 AC 2,439 ms
229,808 KB
testcase_26 AC 2,718 ms
253,668 KB
testcase_27 AC 3,083 ms
334,944 KB
testcase_28 AC 2,833 ms
255,704 KB
testcase_29 AC 2,987 ms
309,300 KB
testcase_30 AC 2,654 ms
253,412 KB
testcase_31 AC 2,690 ms
256,756 KB
testcase_32 AC 2,756 ms
255,196 KB
testcase_33 AC 49 ms
54,528 KB
testcase_34 AC 717 ms
130,064 KB
testcase_35 AC 1,468 ms
383,324 KB
testcase_36 AC 919 ms
112,408 KB
testcase_37 AC 50 ms
54,656 KB
testcase_38 AC 576 ms
77,568 KB
testcase_39 AC 1,747 ms
369,456 KB
testcase_40 AC 1,114 ms
187,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def resolve():
    n, m, q = map(int, input().split())
    edges = [[] for _ in range(n)]
    uf = UnionFind(n)
    for _ in range(m):
        u, v = map(lambda x: int(x) - 1, input().split())
        edges[u].append(v)
        edges[v].append(u)
        uf.union(u, v)
    u = UnionFind(n)
    for v in uf.all_group_members().values():
        d = {j: i for i, j in enumerate(v)}
        edge = [[] for _ in range(len(v))]
        for i, j in enumerate(v):
            for k in edges[j]:
                edge[i].append(d[k])
        bridge, _ = bridge_detection(edge)
        for i, j in bridge:
            u.union(v[i], v[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