結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー hirayuu_ychirayuu_yc
提出日時 2023-11-21 14:30:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,852 ms / 4,000 ms
コード長 2,925 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 216,244 KB
最終ジャッジ日時 2023-11-21 14:31:37
合計ジャッジ時間 45,484 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,608 KB
testcase_01 AC 42 ms
55,608 KB
testcase_02 AC 43 ms
55,608 KB
testcase_03 AC 42 ms
55,608 KB
testcase_04 AC 42 ms
55,608 KB
testcase_05 AC 44 ms
55,608 KB
testcase_06 AC 41 ms
55,608 KB
testcase_07 AC 42 ms
55,608 KB
testcase_08 AC 206 ms
79,276 KB
testcase_09 AC 231 ms
80,768 KB
testcase_10 AC 247 ms
83,992 KB
testcase_11 AC 284 ms
84,476 KB
testcase_12 AC 279 ms
80,644 KB
testcase_13 AC 1,244 ms
131,272 KB
testcase_14 AC 1,301 ms
147,664 KB
testcase_15 AC 1,325 ms
147,548 KB
testcase_16 AC 724 ms
113,228 KB
testcase_17 AC 1,313 ms
156,188 KB
testcase_18 AC 1,131 ms
147,800 KB
testcase_19 AC 1,368 ms
177,972 KB
testcase_20 AC 1,140 ms
144,920 KB
testcase_21 AC 1,299 ms
156,116 KB
testcase_22 AC 1,430 ms
184,888 KB
testcase_23 AC 1,831 ms
196,316 KB
testcase_24 AC 1,799 ms
196,320 KB
testcase_25 AC 1,755 ms
196,504 KB
testcase_26 AC 1,794 ms
196,772 KB
testcase_27 AC 1,852 ms
196,768 KB
testcase_28 AC 1,800 ms
196,356 KB
testcase_29 AC 1,825 ms
196,072 KB
testcase_30 AC 1,774 ms
195,316 KB
testcase_31 AC 1,789 ms
196,068 KB
testcase_32 AC 1,801 ms
195,104 KB
testcase_33 AC 43 ms
55,608 KB
testcase_34 AC 726 ms
106,584 KB
testcase_35 AC 1,052 ms
203,108 KB
testcase_36 AC 1,062 ms
182,876 KB
testcase_37 AC 42 ms
55,608 KB
testcase_38 AC 592 ms
77,276 KB
testcase_39 AC 1,279 ms
213,988 KB
testcase_40 AC 1,258 ms
216,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Graph():
    def __init__(self,size,directed=False):
        self.dir=directed
        self.size=size
        self.gr=[[] for i in range(size)]
        self.edges=[]
    
    def add_edge(self,u,v,status={}):
        self.gr[u].append(self.Edge(u,v,status))
        if not(self.dir):
            self.gr[v].append(self.Edge(v,u,status))
        self.edges.append(self.Edge(u,v,status))

    def node(self,v):
        return self.gr[v]
    
    class Edge():
        def __init__(self,st,to,status):
            self.st=st
            self.to=to
            self.status=status
        
        def __getitem__(self,val):
            return self.status[val]

from collections import deque
def bridge(gr):
    pre=[-1]*gr.size
    low=[-1]*gr.size
    ret=[]
    for i in range(gr.size):
        if pre[i]!=-1:
            continue
        vert=deque([[0,i,-1,-1]])
        cnt=0
        while len(vert)>0:
            step,now,bef,ed=vert[-1]
            if step==1:
                for j in gr.node(now):
                    if j.to!=bef:
                        low[now]=min(low[now],low[j.to])
                if ed!=-1 and pre[now]<=low[now]:
                    ret.append(gr.node(bef)[ed])
                vert.pop()
                continue
            if pre[now]!=-1:
                low[bef]=min(low[bef],low[now])
                vert.pop()
                continue
            pre[now]=cnt
            low[now]=cnt
            cnt+=1
            vert[-1][0]=1
            for j,k in enumerate(gr.node(now)):
                if k.to==bef:
                    continue
                if low[k.to]==-1:
                    vert.append([0,k.to,now,j])
                else:
                    low[now]=min(low[now],low[k.to])
    return ret

class UnionFind():
    def __init__(self,size):
        self.uf=[[-1,0,1] for i in range(size)]
        
    def unite(self,fir,sec):
        one=self.root(fir)
        two=self.root(sec)
        if one!=two:
            if self.uf[one][1]>self.uf[two][1]:
                one,two=two,one
            self.uf[one][0]=two
            self.uf[two][2]+=self.uf[one][2]
            if self.uf[one][1]==self.uf[two][1]:
                self.uf[two][1]+=1
    
    def same(self,fir,sec):
        return self.root(fir)==self.root(sec)
    
    def root(self,node):
        pos=node
        change=[]
        while self.uf[pos][0]!=-1:
            change.append(pos)
            pos=self.uf[pos][0]
        for i in change:
            self.uf[i][0]=pos
        return pos
    
    def size(self,node):
        return self.uf[self.root(node)][2]

N,M,Q=map(int,input().split())
namori=Graph(N)
for i in range(M):
    u,v=map(int,input().split())
    namori.add_edge(u-1,v-1)
union=UnionFind(N)
for i in bridge(namori):
    union.unite(i.st,i.to)
for i in range(Q):
    x,y=map(int,input().split())
    if union.same(x-1,y-1):
        print("Yes")
    else:
        print("No")
0