結果

問題 No.2316 Freight Train
ユーザー navel_tosnavel_tos
提出日時 2023-05-26 21:28:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 740 ms / 2,000 ms
コード長 1,039 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 110,352 KB
最終ジャッジ日時 2023-08-26 10:18:37
合計ジャッジ時間 17,799 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,440 KB
testcase_01 AC 62 ms
71,492 KB
testcase_02 AC 63 ms
71,268 KB
testcase_03 AC 680 ms
109,928 KB
testcase_04 AC 430 ms
92,068 KB
testcase_05 AC 367 ms
92,456 KB
testcase_06 AC 227 ms
78,840 KB
testcase_07 AC 606 ms
83,516 KB
testcase_08 AC 437 ms
110,180 KB
testcase_09 AC 532 ms
97,196 KB
testcase_10 AC 592 ms
92,364 KB
testcase_11 AC 541 ms
106,732 KB
testcase_12 AC 612 ms
103,524 KB
testcase_13 AC 740 ms
110,100 KB
testcase_14 AC 724 ms
110,016 KB
testcase_15 AC 707 ms
110,168 KB
testcase_16 AC 685 ms
110,196 KB
testcase_17 AC 656 ms
110,080 KB
testcase_18 AC 696 ms
110,176 KB
testcase_19 AC 663 ms
110,068 KB
testcase_20 AC 676 ms
110,184 KB
testcase_21 AC 685 ms
109,968 KB
testcase_22 AC 683 ms
110,008 KB
testcase_23 AC 568 ms
110,352 KB
testcase_24 AC 602 ms
110,224 KB
testcase_25 AC 540 ms
108,176 KB
testcase_26 AC 518 ms
108,744 KB
testcase_27 AC 478 ms
78,720 KB
testcase_28 AC 62 ms
71,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder390C

#UnionFind
class UnionFind:
    def __init__(self,N): self._parent=[-1 for i in[0]*N]
    def find(self,v):  #頂点vの親を探し、経路圧縮する
        vertices=[]
        while self._parent[v]>=0: vertices.append(v);v=self._parent[v]
        for i in vertices: self._parent[i]=v
        return v
    def unite(self,x,y):  #頂点xとyを併合し、併合の有無を返す
        x,y = self.find(x),self.find(y)
        if x==y: return 0
        if self._parent[x]>self._parent[y]: x,y=y,x  #負値で管理
        self._parent[x]+=self._parent[y]; self._parent[y]=x; return 1
    def same(self,x,y):return self.find(x)==self.find(y)   #xとyは同一集合か返す
    def size(self,x):  return -self._parent[self.find(x)]  #xの集合のサイズを求める


f=lambda:list(map(int,input().split()))
N,Q=f(); P=f(); UF=UnionFind(N)
for i in range(N):
    x=P[i]
    if x==-1: continue
    x-=1
    UF.unite(i,x)
for _ in range(Q):
    a,b=f(); a-=1; b-=1
    print('Yes') if UF.same(a,b) else print('No')
0