結果
問題 | No.2316 Freight Train |
ユーザー | yabit |
提出日時 | 2023-06-19 12:21:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 980 ms / 2,000 ms |
コード長 | 1,765 bytes |
コンパイル時間 | 921 ms |
コンパイル使用メモリ | 86,036 KB |
実行使用メモリ | 109,948 KB |
最終ジャッジ日時 | 2023-09-09 07:26:35 |
合計ジャッジ時間 | 26,951 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 95 ms
71,592 KB |
testcase_01 | AC | 96 ms
71,536 KB |
testcase_02 | AC | 94 ms
71,476 KB |
testcase_03 | AC | 958 ms
109,056 KB |
testcase_04 | AC | 727 ms
92,184 KB |
testcase_05 | AC | 582 ms
92,036 KB |
testcase_06 | AC | 309 ms
81,492 KB |
testcase_07 | AC | 770 ms
83,872 KB |
testcase_08 | AC | 742 ms
109,504 KB |
testcase_09 | AC | 807 ms
96,968 KB |
testcase_10 | AC | 787 ms
92,304 KB |
testcase_11 | AC | 776 ms
106,136 KB |
testcase_12 | AC | 828 ms
102,496 KB |
testcase_13 | AC | 960 ms
109,792 KB |
testcase_14 | AC | 961 ms
109,740 KB |
testcase_15 | AC | 953 ms
109,940 KB |
testcase_16 | AC | 941 ms
109,780 KB |
testcase_17 | AC | 952 ms
109,948 KB |
testcase_18 | AC | 980 ms
109,744 KB |
testcase_19 | AC | 931 ms
109,480 KB |
testcase_20 | AC | 948 ms
109,768 KB |
testcase_21 | AC | 954 ms
109,856 KB |
testcase_22 | AC | 935 ms
109,740 KB |
testcase_23 | AC | 579 ms
109,172 KB |
testcase_24 | AC | 605 ms
109,368 KB |
testcase_25 | AC | 594 ms
107,964 KB |
testcase_26 | AC | 561 ms
108,080 KB |
testcase_27 | AC | 505 ms
78,028 KB |
testcase_28 | AC | 91 ms
71,520 KB |
ソースコード
from collections import defaultdict # UnionFind(n) n:頂点数 # parents:i番目の点が属する根 それ自身が根なら-1 # find(x):xの属する根を再帰的に求める # unite(x,y):多い木に小さい木を結合 class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N,Q = map(int, input().split()) P = list(map(int, input().split())) uf = UnionFind(N) for i in range(N): if P[i] > -1: uf.union(P[i]-1, i) for i in range(Q): a,b = map(int, input().split()) if uf.same(a-1, b-1): print('Yes') else: print('No')