結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー 👑 rin204rin204
提出日時 2022-06-24 17:08:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,219 ms / 4,000 ms
コード長 3,292 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 147,600 KB
最終ジャッジ日時 2024-11-08 11:00:10
合計ジャッジ時間 31,959 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

def LowLink(n, edges):
    ord = [-1] * n
    low = [-1] * n
    isartic = [False] * n
    bridge = []
    k = 0

    def dfs(root, k):
        stack = [(~root, -1), (root, -1)]
        cnt = 0
        while stack:
            pos, bpos = stack.pop()
            if pos >= 0:
                if bpos != -1 and ord[pos] != -1:
                    low[bpos] = min(low[bpos], ord[pos])
                    stack.pop()
                    continue
                low[pos] = k
                ord[pos] = k
                k += 1
                for npos in edges[pos]:
                    if npos == bpos:
                        continue
                    if ord[npos] == -1:
                        if bpos == -1:
                            cnt += 1
                        stack.append((~npos, pos))
                        stack.append((npos, pos))
                    else:
                        low[pos] = min(low[pos], ord[npos])
                        if npos == root:
                            cnt -= 1
            else:
                pos = ~pos
                if ord[bpos] < low[pos]:
                    if bpos != -1:
                        bridge.append((min(bpos, pos), max(bpos, pos)))
                
                if low[pos] >= ord[bpos] and bpos != root and bpos != -1:
                    isartic[bpos] = True

                if bpos != -1:
                    low[bpos] = min(low[bpos], low[pos])

        if cnt >= 2:
            isartic[root] = True
        
        return k

    for i in range(n):
        if ord[i] == -1:
            k = dfs(i, k)

    return isartic, bridge

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

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

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

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

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m, Q = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append(v)
    edges[v].append(u)

_, bridge = LowLink(n, edges)
UF = UnionFind(n)
for u, v in bridge:
    UF.union(u, v)

for _ in range(Q):
    x, y = map(int, input().split())
    if UF.same(x - 1, y - 1):
        print("Yes")
    else:
        print("No")
0