結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-06-21 18:55:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,234 ms / 4,000 ms
コード長 2,202 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 139,320 KB
最終ジャッジ日時 2024-04-22 14:06:51
合計ジャッジ時間 31,530 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,608 KB
testcase_01 AC 42 ms
52,224 KB
testcase_02 AC 42 ms
52,352 KB
testcase_03 AC 43 ms
52,224 KB
testcase_04 AC 42 ms
52,608 KB
testcase_05 AC 42 ms
52,736 KB
testcase_06 AC 42 ms
52,736 KB
testcase_07 AC 47 ms
52,608 KB
testcase_08 AC 188 ms
78,480 KB
testcase_09 AC 202 ms
78,976 KB
testcase_10 AC 216 ms
78,848 KB
testcase_11 AC 217 ms
79,576 KB
testcase_12 AC 229 ms
78,504 KB
testcase_13 AC 821 ms
95,280 KB
testcase_14 AC 860 ms
98,816 KB
testcase_15 AC 968 ms
107,008 KB
testcase_16 AC 520 ms
93,184 KB
testcase_17 AC 868 ms
101,760 KB
testcase_18 AC 840 ms
107,392 KB
testcase_19 AC 898 ms
111,132 KB
testcase_20 AC 880 ms
105,600 KB
testcase_21 AC 850 ms
102,196 KB
testcase_22 AC 884 ms
107,136 KB
testcase_23 AC 1,201 ms
111,932 KB
testcase_24 AC 1,234 ms
112,000 KB
testcase_25 AC 1,216 ms
112,208 KB
testcase_26 AC 1,187 ms
112,448 KB
testcase_27 AC 1,162 ms
112,548 KB
testcase_28 AC 1,178 ms
112,460 KB
testcase_29 AC 1,225 ms
112,296 KB
testcase_30 AC 1,187 ms
112,152 KB
testcase_31 AC 1,202 ms
112,188 KB
testcase_32 AC 1,204 ms
112,804 KB
testcase_33 AC 41 ms
52,352 KB
testcase_34 AC 585 ms
90,752 KB
testcase_35 AC 793 ms
112,852 KB
testcase_36 AC 817 ms
105,984 KB
testcase_37 AC 42 ms
52,224 KB
testcase_38 AC 562 ms
77,440 KB
testcase_39 AC 835 ms
130,600 KB
testcase_40 AC 866 ms
139,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

def Lowlink(graph):
    n = len(graph)
    order = [-1]*n
    low = [n]*n
    par = [-1]*n
    vis = 0
    for root in range(n):
        if order[root] != -1:
            continue
        q = [root]
        while q:
            now = q.pop()

            if now < 0:
                now = ~now
                if now == root:
                    continue
                p = par[now]
                low[p] = min(low[p],low[now])
                continue

            if order[now] >= 0:
                continue
            order[now] = vis
            low[now] = vis
            vis += 1
            q.append(~now)

            check = 0
            for nex in graph[now]:
                if par[now] == nex and check == 0:
                    check += 1
                    continue
                elif order[nex] >= 0:
                    if low[now] < 0 or order[nex] < low[now]:
                        low[now] = order[nex]
                else:
                    par[nex] = now
                    q.append(nex)

    briges = []
    for i in range(n):
        if par[i] < 0:
            continue
        if low[i] < 0 or low[i] > order[par[i]]:
            briges.append((i,par[i]))

    return briges


n,m,q = map(int,input().split())
e = [[] for i in range(n)]
for _ in range(m):
    u,v = [int(x)-1 for x in input().split()]
    e[u].append(v)
    e[v].append(u)

briges = Lowlink(e)

uf = Unionfind(n)
for u,v in briges:
    uf.union(u,v)

for _ in range(q):
    x,y = [int(i)-1 for i in input().split()]
    print("Yes" if uf.same(x,y) else "No")
0