結果
問題 | No.2316 Freight Train |
ユーザー | Shirotsume |
提出日時 | 2023-05-26 21:44:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 548 ms / 2,000 ms |
コード長 | 2,215 bytes |
コンパイル時間 | 434 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 108,160 KB |
最終ジャッジ日時 | 2024-06-07 06:15:47 |
合計ジャッジ時間 | 13,510 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
53,888 KB |
testcase_01 | AC | 42 ms
54,400 KB |
testcase_02 | AC | 43 ms
54,144 KB |
testcase_03 | AC | 513 ms
107,972 KB |
testcase_04 | AC | 390 ms
90,320 KB |
testcase_05 | AC | 376 ms
90,552 KB |
testcase_06 | AC | 206 ms
78,796 KB |
testcase_07 | AC | 349 ms
82,424 KB |
testcase_08 | AC | 480 ms
107,776 KB |
testcase_09 | AC | 446 ms
95,232 KB |
testcase_10 | AC | 430 ms
90,752 KB |
testcase_11 | AC | 462 ms
104,428 KB |
testcase_12 | AC | 468 ms
101,376 KB |
testcase_13 | AC | 497 ms
108,032 KB |
testcase_14 | AC | 488 ms
107,520 KB |
testcase_15 | AC | 497 ms
107,520 KB |
testcase_16 | AC | 512 ms
107,776 KB |
testcase_17 | AC | 548 ms
107,776 KB |
testcase_18 | AC | 527 ms
108,160 KB |
testcase_19 | AC | 527 ms
107,520 KB |
testcase_20 | AC | 509 ms
107,648 KB |
testcase_21 | AC | 509 ms
107,648 KB |
testcase_22 | AC | 515 ms
107,648 KB |
testcase_23 | AC | 180 ms
108,032 KB |
testcase_24 | AC | 197 ms
107,520 KB |
testcase_25 | AC | 180 ms
105,984 KB |
testcase_26 | AC | 165 ms
105,728 KB |
testcase_27 | AC | 121 ms
76,416 KB |
testcase_28 | AC | 41 ms
54,144 KB |
ソースコード
import sys from collections import deque, Counter input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.num = N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) x=self.leader(a) y=self.leader(b) if x==y: return x self.num -= 1 if (-self.parent_or_size[x]<-self.parent_or_size[y]): x,y=y,x self.parent_or_size[x]+=self.parent_or_size[y] self.parent_or_size[y]=x return x def same(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) return self.leader(a)==self.leader(b) def leader(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) if (self.parent_or_size[a]<0): return a self.parent_or_size[a]=self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf=[0 for i in range(self.n)] group_size=[0 for i in range(self.n)] for i in range(self.n): leader_buf[i]=self.leader(i) group_size[leader_buf[i]]+=1 result=[[] for i in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2=[] for i in range(self.n): if len(result[i])>0: result2.append(result[i]) return result2 def count(self): return self.num n, q = mi() p = li() U = dsu(n) for i in range(n): if p[i] == -1: continue else: U.merge(i, p[i] - 1) for _ in range(q): a, b = mi() a -= 1; b -= 1 print('Yes' if U.same(a, b) else 'No')