結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー strangerxxxstrangerxxx
提出日時 2022-09-16 16:55:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,596 ms / 4,000 ms
コード長 4,113 bytes
コンパイル時間 398 ms
コンパイル使用メモリ 11,492 KB
実行使用メモリ 147,648 KB
最終ジャッジ日時 2023-08-23 11:19:38
合計ジャッジ時間 54,131 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
9,132 KB
testcase_01 AC 20 ms
9,004 KB
testcase_02 AC 20 ms
8,936 KB
testcase_03 AC 20 ms
9,068 KB
testcase_04 AC 20 ms
9,088 KB
testcase_05 AC 21 ms
9,084 KB
testcase_06 AC 21 ms
9,092 KB
testcase_07 AC 20 ms
9,064 KB
testcase_08 AC 43 ms
10,136 KB
testcase_09 AC 77 ms
11,364 KB
testcase_10 AC 83 ms
14,160 KB
testcase_11 AC 85 ms
13,616 KB
testcase_12 AC 55 ms
11,076 KB
testcase_13 AC 1,212 ms
60,760 KB
testcase_14 AC 1,457 ms
72,068 KB
testcase_15 AC 1,592 ms
63,064 KB
testcase_16 AC 1,105 ms
53,600 KB
testcase_17 AC 1,531 ms
75,604 KB
testcase_18 AC 1,424 ms
59,500 KB
testcase_19 AC 2,114 ms
108,428 KB
testcase_20 AC 1,477 ms
60,588 KB
testcase_21 AC 1,687 ms
84,660 KB
testcase_22 AC 1,901 ms
92,840 KB
testcase_23 AC 2,520 ms
115,348 KB
testcase_24 AC 2,504 ms
114,744 KB
testcase_25 AC 2,511 ms
114,776 KB
testcase_26 AC 2,596 ms
115,328 KB
testcase_27 AC 2,559 ms
114,840 KB
testcase_28 AC 2,551 ms
114,720 KB
testcase_29 AC 2,524 ms
115,308 KB
testcase_30 AC 2,521 ms
114,728 KB
testcase_31 AC 2,478 ms
114,892 KB
testcase_32 AC 2,443 ms
114,664 KB
testcase_33 AC 20 ms
8,988 KB
testcase_34 AC 1,233 ms
63,480 KB
testcase_35 AC 1,363 ms
120,908 KB
testcase_36 AC 1,240 ms
58,624 KB
testcase_37 AC 21 ms
9,044 KB
testcase_38 AC 382 ms
8,948 KB
testcase_39 AC 1,551 ms
147,648 KB
testcase_40 AC 1,591 ms
141,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def resolve():
    import sys
    input = sys.stdin.readline
    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(edges, start=0):
    n = len(edges)
    ord = [-1] * n
    order = ord[:]
    low = ord[:]
    q = [(start, -1)]
    cnt = 0
    edge = [[] for _ in range(n)]
    while q:
        i, pos = q.pop()
        if ord[i] >= 0:
            edge[i].append(pos)
            continue
        if pos >= 0:
            edge[pos].append(i)
        ord[i] = cnt
        order[cnt] = i
        cnt += 1
        for j in reversed(edges[i]):
            if j == pos or ord[j] >= 0:
                continue
            q.append((j, i))
    for i, j in enumerate(reversed(order)):
        low[j] = n - i - 1
        for k in edge[j]:
            if ord[k] < low[j]:
                low[j] = ord[k]
            if low[k] >= 0 and low[k] < low[j]:
                low[j] = low[k]
    bridges = []
    for i, j in enumerate(edge):
        for k in j:
            if ord[i] < low[k]:
                bridges.append((i, k))
    return bridges


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