結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2021-01-22 08:44:52 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 531 ms / 3,000 ms |
コード長 | 2,359 bytes |
コンパイル時間 | 2,811 ms |
コンパイル使用メモリ | 75,412 KB |
実行使用メモリ | 70,740 KB |
最終ジャッジ日時 | 2023-08-26 23:10:50 |
合計ジャッジ時間 | 13,359 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 114 ms
56,084 KB |
testcase_01 | AC | 115 ms
54,212 KB |
testcase_02 | AC | 139 ms
56,420 KB |
testcase_03 | AC | 289 ms
61,452 KB |
testcase_04 | AC | 531 ms
70,716 KB |
testcase_05 | AC | 496 ms
70,108 KB |
testcase_06 | AC | 497 ms
70,316 KB |
testcase_07 | AC | 477 ms
70,724 KB |
testcase_08 | AC | 474 ms
70,604 KB |
testcase_09 | AC | 485 ms
68,404 KB |
testcase_10 | AC | 494 ms
70,740 KB |
testcase_11 | AC | 501 ms
70,464 KB |
testcase_12 | AC | 482 ms
70,608 KB |
testcase_13 | AC | 466 ms
70,272 KB |
testcase_14 | AC | 480 ms
70,660 KB |
testcase_15 | AC | 478 ms
70,384 KB |
testcase_16 | AC | 481 ms
70,172 KB |
testcase_17 | AC | 464 ms
70,472 KB |
testcase_18 | AC | 475 ms
68,148 KB |
testcase_19 | AC | 471 ms
70,316 KB |
testcase_20 | AC | 115 ms
56,052 KB |
testcase_21 | AC | 114 ms
56,148 KB |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int length = sc.nextInt(); char[][] all = new char[n][]; for (int i = 0; i < n; i++) { all[i] = sc.next().toCharArray(); } PriorityQueue<Path> queue = new PriorityQueue<>(); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { queue.add(new Path(i, j, getCost(all[i], all[j]))); } } UnionFindTree uft = new UnionFindTree(n); long cost = 0; while (queue.size() > 0) { Path p = queue.poll(); if (!uft.same(p)) { uft.unite(p); cost += p.value; } } System.out.println(cost); } static int getCost(char[] a1, char[] a2) { int cost = 0; for (int i = 0; i < a1.length; i++) { if (a1[i] != a2[i]) { cost++; } } return cost; } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (x == parents[x]) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public boolean same(Path p) { return same(p.left, p.right); } public void unite(int x, int y) { parents[find(x)] = find(y); } public void unite(Path p) { unite(p.left, p.right); } } static class Path implements Comparable<Path> { int left; int right; int value; public Path(int left, int right, int value) { this.left = left; this.right = right; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }