結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2021-11-24 15:43:51 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 484 ms / 3,000 ms |
コード長 | 3,071 bytes |
コンパイル時間 | 2,263 ms |
コンパイル使用メモリ | 75,260 KB |
実行使用メモリ | 73,824 KB |
最終ジャッジ日時 | 2023-09-09 07:59:28 |
合計ジャッジ時間 | 12,301 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
49,484 KB |
testcase_01 | AC | 44 ms
49,544 KB |
testcase_02 | AC | 75 ms
51,956 KB |
testcase_03 | AC | 198 ms
57,896 KB |
testcase_04 | AC | 454 ms
73,312 KB |
testcase_05 | AC | 460 ms
73,636 KB |
testcase_06 | AC | 465 ms
73,552 KB |
testcase_07 | AC | 443 ms
73,384 KB |
testcase_08 | AC | 428 ms
73,196 KB |
testcase_09 | AC | 455 ms
73,224 KB |
testcase_10 | AC | 455 ms
73,464 KB |
testcase_11 | AC | 484 ms
73,824 KB |
testcase_12 | AC | 451 ms
73,480 KB |
testcase_13 | AC | 439 ms
73,804 KB |
testcase_14 | AC | 446 ms
73,668 KB |
testcase_15 | AC | 450 ms
73,324 KB |
testcase_16 | AC | 461 ms
73,472 KB |
testcase_17 | AC | 458 ms
73,440 KB |
testcase_18 | AC | 470 ms
73,396 KB |
testcase_19 | AC | 454 ms
73,192 KB |
testcase_20 | AC | 44 ms
49,468 KB |
testcase_21 | AC | 44 ms
49,476 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int length = sc.nextInt(); char[][] dnas = new char[n][]; for (int i = 0; i < n; i++) { dnas[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++) { int count = 0; for (int k = 0; k < length; k++) { if (dnas[i][k] != dnas[j][k]) { count++; } } queue.add(new Path(count, i, j)); } } int ans = 0; UnionFindTree uft = new UnionFindTree(n); while (queue.size() > 0) { Path p = queue.poll(); if (uft.same(p)) { continue; } ans += p.cost; uft.unite(p); } System.out.println(ans); } 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) { if (!same(x, y)) { parents[find(y)] = find(x); } } public void unite(Path p) { unite(p.left, p.right); } } static class Path implements Comparable<Path> { int cost; int left; int right; public Path(int cost, int left, int right) { this.cost = cost; this.left = left; this.right = right; } public int compareTo(Path another) { return cost - another.cost; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }