結果
問題 | No.2563 色ごとのグループ |
ユーザー | hiro1729 |
提出日時 | 2024-10-12 12:41:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 466 ms / 2,000 ms |
コード長 | 2,546 bytes |
コンパイル時間 | 332 ms |
コンパイル使用メモリ | 82,372 KB |
実行使用メモリ | 130,772 KB |
最終ジャッジ日時 | 2024-10-12 12:41:59 |
合計ジャッジ時間 | 10,706 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
68,980 KB |
testcase_01 | AC | 63 ms
67,996 KB |
testcase_02 | AC | 65 ms
67,816 KB |
testcase_03 | AC | 62 ms
68,240 KB |
testcase_04 | AC | 63 ms
67,828 KB |
testcase_05 | AC | 63 ms
67,648 KB |
testcase_06 | AC | 61 ms
68,172 KB |
testcase_07 | AC | 63 ms
67,828 KB |
testcase_08 | AC | 63 ms
67,876 KB |
testcase_09 | AC | 63 ms
69,820 KB |
testcase_10 | AC | 63 ms
68,188 KB |
testcase_11 | AC | 65 ms
69,116 KB |
testcase_12 | AC | 64 ms
70,080 KB |
testcase_13 | AC | 62 ms
69,956 KB |
testcase_14 | AC | 109 ms
78,968 KB |
testcase_15 | AC | 106 ms
79,140 KB |
testcase_16 | AC | 108 ms
78,664 KB |
testcase_17 | AC | 104 ms
78,976 KB |
testcase_18 | AC | 80 ms
74,868 KB |
testcase_19 | AC | 109 ms
78,856 KB |
testcase_20 | AC | 118 ms
81,072 KB |
testcase_21 | AC | 114 ms
79,108 KB |
testcase_22 | AC | 115 ms
79,372 KB |
testcase_23 | AC | 116 ms
79,244 KB |
testcase_24 | AC | 221 ms
94,320 KB |
testcase_25 | AC | 213 ms
100,240 KB |
testcase_26 | AC | 268 ms
114,068 KB |
testcase_27 | AC | 252 ms
128,748 KB |
testcase_28 | AC | 281 ms
118,600 KB |
testcase_29 | AC | 362 ms
130,340 KB |
testcase_30 | AC | 340 ms
130,272 KB |
testcase_31 | AC | 337 ms
130,772 KB |
testcase_32 | AC | 373 ms
130,452 KB |
testcase_33 | AC | 466 ms
117,412 KB |
testcase_34 | AC | 462 ms
117,840 KB |
testcase_35 | AC | 462 ms
118,092 KB |
testcase_36 | AC | 441 ms
117,796 KB |
testcase_37 | AC | 466 ms
117,032 KB |
ソースコード
# import pypyjit # pypyjit.set_param('max_unroll_recursion=-1') from collections import defaultdict as dd S = input R = range P = print def I(): return int(S()) def M(): return map(int, S().split()) def L(): return list(M()) def O(): return list(map(int, open(0).read().split())) def yn(b): print("Yes" if b else "No") biga = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" smaa = "abcdefghijklmnopqrstuvwxyz" ctoi = lambda c: ord(c) - ord('a') itoc = lambda i: chr(ord('a') + i) inf = 10 ** 18 mod = 998244353 def acc(a): b = [0] for i in a: b.append(b[-1] + i) return b import typing class DSU: ''' Implement (union by size) + (path halving) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a < self._n assert 0 <= b < self._n x = self.leader(a) y = self.leader(b) if x == y: return x 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: int, b: int) -> bool: assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a: int) -> int: assert 0 <= a < self._n parent = self.parent_or_size[a] while parent >= 0: if self.parent_or_size[parent] < 0: return parent self.parent_or_size[a], a, parent = ( self.parent_or_size[parent], self.parent_or_size[parent], self.parent_or_size[self.parent_or_size[parent]] ) return a def size(self, a: int) -> int: assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self) -> typing.List[typing.List[int]]: leader_buf = [self.leader(i) for i in range(self._n)] result: typing.List[typing.List[int]] = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return list(filter(lambda r: r, result)) n,m=M() c=L() uf=DSU(n) for _ in R(m): a,b=M() if c[a-1]==c[b-1]: uf.merge(a-1,b-1) cnt=[0]*n for i in uf.groups(): s=set() for j in i:s.add(c[j]) for j in list(s):cnt[j-1]+=1 P(sum(max(0,i-1)for i in cnt))