結果
問題 | No.2923 Mayor's Job |
ユーザー | amesyu |
提出日時 | 2024-10-12 18:29:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 143 ms / 2,000 ms |
コード長 | 1,893 bytes |
コンパイル時間 | 740 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 77,440 KB |
最終ジャッジ日時 | 2024-10-12 18:29:31 |
合計ジャッジ時間 | 3,278 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
54,016 KB |
testcase_01 | AC | 47 ms
53,888 KB |
testcase_02 | AC | 45 ms
54,144 KB |
testcase_03 | AC | 143 ms
77,056 KB |
testcase_04 | AC | 120 ms
77,056 KB |
testcase_05 | AC | 135 ms
77,184 KB |
testcase_06 | AC | 133 ms
77,184 KB |
testcase_07 | AC | 129 ms
77,440 KB |
testcase_08 | AC | 134 ms
77,184 KB |
testcase_09 | AC | 137 ms
77,184 KB |
testcase_10 | AC | 138 ms
77,184 KB |
testcase_11 | AC | 120 ms
77,184 KB |
testcase_12 | AC | 119 ms
76,928 KB |
testcase_13 | AC | 72 ms
67,456 KB |
testcase_14 | AC | 61 ms
64,128 KB |
testcase_15 | AC | 46 ms
54,016 KB |
testcase_16 | AC | 46 ms
54,016 KB |
testcase_17 | AC | 46 ms
54,400 KB |
testcase_18 | AC | 45 ms
54,656 KB |
testcase_19 | AC | 46 ms
54,144 KB |
ソースコード
from collections import defaultdict 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, k = map(int, input().split()) h = list(map(int, input().split())) uf = UnionFind(n) def distance2(A, B): x1, y1 = A x2, y2 = B return (x2-x1)**2 + (y2-y1)**2 p = [] for _ in range(n): x, y = map(int, input().split()) p.append((x, y)) search = list(enumerate(h)) search.sort(key = lambda x: x[1]) use = [0] * n # print(search) for i, val in search: if use[i]: continue for j in range(n): if use[j]: continue if h[j] >= h[i]: continue if distance2(p[i], p[j]) > k*k: continue uf.union(i, j) use[j] = 1 print(uf.group_count())