結果
問題 | No.1166 NADA DNA |
ユーザー | Mitarushi |
提出日時 | 2020-08-11 15:38:57 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 2,213 ms / 3,000 ms |
コード長 | 1,181 bytes |
コンパイル時間 | 326 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 63,788 KB |
最終ジャッジ日時 | 2024-04-17 17:20:32 |
合計ジャッジ時間 | 35,842 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 29 ms
10,880 KB |
testcase_01 | AC | 30 ms
10,880 KB |
testcase_02 | AC | 44 ms
11,136 KB |
testcase_03 | AC | 529 ms
23,120 KB |
testcase_04 | AC | 2,058 ms
63,272 KB |
testcase_05 | AC | 2,077 ms
63,684 KB |
testcase_06 | AC | 2,053 ms
63,680 KB |
testcase_07 | AC | 2,065 ms
63,560 KB |
testcase_08 | AC | 2,063 ms
63,684 KB |
testcase_09 | AC | 2,119 ms
63,784 KB |
testcase_10 | AC | 2,173 ms
63,268 KB |
testcase_11 | AC | 2,154 ms
63,788 KB |
testcase_12 | AC | 2,168 ms
63,784 KB |
testcase_13 | AC | 2,168 ms
63,680 KB |
testcase_14 | AC | 2,112 ms
63,784 KB |
testcase_15 | AC | 2,159 ms
63,552 KB |
testcase_16 | AC | 2,169 ms
63,276 KB |
testcase_17 | AC | 2,213 ms
63,784 KB |
testcase_18 | AC | 2,116 ms
63,788 KB |
testcase_19 | AC | 2,179 ms
63,680 KB |
testcase_20 | AC | 29 ms
10,880 KB |
testcase_21 | AC | 29 ms
10,880 KB |
ソースコード
n, l = [int(i) for i in input().split()] g = [input() for _ in range(n)] def distant(a, b): c = 0 for i, j in zip(a, b): c += i != j return c class UnionFind: def __init__(self, n): self.parent = [-1] * n self.rank = [1] * n def get_root(self, i): while self.parent[i] != -1: i = self.parent[i] return i def is_same_tree(self, i, j): return self.get_root(i) == self.get_root(j) def merge(self, i, j): i = self.get_root(i) j = self.get_root(j) if i == j: return if self.rank[i] > self.rank[j]: self.parent[j] = i self.rank[i] = max(self.rank[i], self.rank[j] + 1) else: self.parent[i] = j self.rank[j] = max(self.rank[j], self.rank[i] + 1) uf = UnionFind(n) cost_vec = [(distant(g[i], g[j]), i, j) for i in range(n) for j in range(i + 1, n)] cost_vec.sort() cost = 0 index = 0 for _ in range(n - 1): while True: c, i, j = cost_vec[index] index += 1 if not uf.is_same_tree(i, j): break cost += c uf.merge(i, j) print(cost)