結果

問題 No.2316 Freight Train
ユーザー 👑 timitimi
提出日時 2023-05-26 21:27:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,002 ms / 2,000 ms
コード長 720 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 114,796 KB
最終ジャッジ日時 2023-08-26 10:15:59
合計ジャッジ時間 22,934 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,128 KB
testcase_01 AC 73 ms
71,388 KB
testcase_02 AC 73 ms
71,260 KB
testcase_03 AC 970 ms
114,296 KB
testcase_04 AC 670 ms
94,680 KB
testcase_05 AC 576 ms
94,028 KB
testcase_06 AC 294 ms
79,448 KB
testcase_07 AC 775 ms
84,268 KB
testcase_08 AC 732 ms
114,652 KB
testcase_09 AC 794 ms
100,080 KB
testcase_10 AC 806 ms
94,636 KB
testcase_11 AC 794 ms
111,076 KB
testcase_12 AC 862 ms
106,784 KB
testcase_13 AC 979 ms
114,660 KB
testcase_14 AC 1,002 ms
114,716 KB
testcase_15 AC 980 ms
114,644 KB
testcase_16 AC 996 ms
114,604 KB
testcase_17 AC 959 ms
114,472 KB
testcase_18 AC 972 ms
114,540 KB
testcase_19 AC 982 ms
114,468 KB
testcase_20 AC 983 ms
114,572 KB
testcase_21 AC 961 ms
114,604 KB
testcase_22 AC 967 ms
114,744 KB
testcase_23 AC 558 ms
114,620 KB
testcase_24 AC 589 ms
114,796 KB
testcase_25 AC 578 ms
113,248 KB
testcase_26 AC 545 ms
112,860 KB
testcase_27 AC 505 ms
78,420 KB
testcase_28 AC 72 ms
71,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,Q=map(int, input().split())
A=list(map(int, input().split()))

par=[i for i in range(N)]
rank=[0]*(N)
friend=[0]*N
block=[0]*N
size=[1]*N
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 union(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return 
  if rank[x]>rank[y]:
    par[y]=x
    size[x]+=size[y]
  else:
    par[x]=y
    size[y]+=size[x]
    if rank[x]==rank[y]:
      rank[y]+=1
 
 
for a in range(N):
  #a,b=map(int,input().split())
  b=A[a]
  if b!=-1:
    union(a,b-1)
    
for _ in range(Q):
  a,b=map(int,input().split())
  a-=1;b-=1
  if same(a,b):
    print('Yes')
  else:
    print('No')
0