結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー 👑 rin204rin204
提出日時 2022-06-24 17:08:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,003 ms / 4,000 ms
コード長 3,292 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 147,728 KB
最終ジャッジ日時 2024-04-25 22:51:44
合計ジャッジ時間 26,591 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
52,864 KB
testcase_01 AC 34 ms
54,480 KB
testcase_02 AC 34 ms
53,012 KB
testcase_03 AC 34 ms
52,864 KB
testcase_04 AC 34 ms
53,084 KB
testcase_05 AC 34 ms
53,376 KB
testcase_06 AC 35 ms
53,248 KB
testcase_07 AC 34 ms
53,120 KB
testcase_08 AC 138 ms
78,484 KB
testcase_09 AC 172 ms
79,184 KB
testcase_10 AC 171 ms
79,068 KB
testcase_11 AC 182 ms
79,020 KB
testcase_12 AC 184 ms
78,644 KB
testcase_13 AC 662 ms
95,744 KB
testcase_14 AC 702 ms
102,144 KB
testcase_15 AC 821 ms
105,660 KB
testcase_16 AC 482 ms
93,656 KB
testcase_17 AC 685 ms
102,560 KB
testcase_18 AC 716 ms
106,700 KB
testcase_19 AC 732 ms
112,392 KB
testcase_20 AC 741 ms
104,808 KB
testcase_21 AC 687 ms
103,336 KB
testcase_22 AC 723 ms
111,488 KB
testcase_23 AC 937 ms
117,372 KB
testcase_24 AC 951 ms
117,760 KB
testcase_25 AC 987 ms
116,740 KB
testcase_26 AC 987 ms
118,164 KB
testcase_27 AC 1,003 ms
116,932 KB
testcase_28 AC 964 ms
118,508 KB
testcase_29 AC 974 ms
117,096 KB
testcase_30 AC 957 ms
118,556 KB
testcase_31 AC 957 ms
117,260 KB
testcase_32 AC 961 ms
116,864 KB
testcase_33 AC 33 ms
53,624 KB
testcase_34 AC 519 ms
90,856 KB
testcase_35 AC 623 ms
117,016 KB
testcase_36 AC 653 ms
105,800 KB
testcase_37 AC 33 ms
53,948 KB
testcase_38 AC 481 ms
77,508 KB
testcase_39 AC 668 ms
135,172 KB
testcase_40 AC 692 ms
147,728 KB
権限があれば一括ダウンロードができます

ソースコード

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