結果

問題 No.2316 Freight Train
ユーザー miho-4miho-4
提出日時 2023-05-26 21:33:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 694 ms / 2,000 ms
コード長 688 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 86,840 KB
実行使用メモリ 110,048 KB
最終ジャッジ日時 2023-08-26 10:32:49
合計ジャッジ時間 17,002 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,244 KB
testcase_01 AC 68 ms
71,444 KB
testcase_02 AC 68 ms
71,316 KB
testcase_03 AC 667 ms
108,020 KB
testcase_04 AC 508 ms
96,104 KB
testcase_05 AC 472 ms
92,356 KB
testcase_06 AC 260 ms
83,588 KB
testcase_07 AC 508 ms
104,592 KB
testcase_08 AC 571 ms
107,272 KB
testcase_09 AC 587 ms
101,996 KB
testcase_10 AC 563 ms
104,104 KB
testcase_11 AC 586 ms
103,936 KB
testcase_12 AC 631 ms
105,180 KB
testcase_13 AC 666 ms
108,332 KB
testcase_14 AC 686 ms
110,048 KB
testcase_15 AC 681 ms
108,368 KB
testcase_16 AC 691 ms
109,756 KB
testcase_17 AC 694 ms
108,148 KB
testcase_18 AC 686 ms
109,628 KB
testcase_19 AC 679 ms
108,292 KB
testcase_20 AC 687 ms
108,040 KB
testcase_21 AC 690 ms
108,704 KB
testcase_22 AC 670 ms
108,548 KB
testcase_23 AC 313 ms
108,184 KB
testcase_24 AC 321 ms
108,128 KB
testcase_25 AC 305 ms
106,300 KB
testcase_26 AC 294 ms
106,140 KB
testcase_27 AC 253 ms
98,020 KB
testcase_28 AC 68 ms
71,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,q=map(int,input().split())
L=list(map(int,input().split()))
Q=[list(map(int,input().split())) for _ in range(q)]

import sys
sys.setrecursionlimit(10**8)

par=[i for i in range(n+1)]
rank=[0]*(n+1)

def find(x):
  if par[x]==x:
    return x
  else:
    par[x]=find(par[x])
    return par[x]

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

def unit(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return 0
  if rank[x]<rank[y]:
    par[x]=y
  else:
    par[y]=x
    if rank[x]==rank[y]:
      rank[x]+=1
    
for i in range(1,1+n):
  if L[i-1]==-1:
    L[i-1]=i
for i in range(1,1+n):
  if L[i-1]!=i:
    unit(i,L[i-1])
    
for a,b in Q:
  if same(a,b):
    print("Yes")
  else:
    print("No")
0