結果
問題 |
No.2316 Freight Train
|
ユーザー |
![]() |
提出日時 | 2023-05-27 11:25:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,113 ms / 2,000 ms |
コード長 | 619 bytes |
コンパイル時間 | 354 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 107,776 KB |
最終ジャッジ日時 | 2024-12-25 21:15:29 |
合計ジャッジ時間 | 27,416 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
def find(x): if P[x] == x: return x else: P[x] = find(P[x]) #経路圧縮 return P[x] # return find(P[x]) #経路圧縮なし def same(x,y): return find(x) == find(y) def unite(x,y): x = find(x) y = find(y) if x == y: return 0 if x > y: x,y = y,x P[y] = x N,Q = map(int,input().split()) PI = list(map(int,input().split())) P = [i for i in range(N+1)] for i,p in enumerate(PI,start = 1): if p == -1: continue unite(i,p) for _ in range(Q): a,b = map(int,input().split()) if same(a,b): print("Yes") else: print("No")