結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2021-11-24 15:43:51 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 463 ms / 3,000 ms |
コード長 | 3,071 bytes |
コンパイル時間 | 2,598 ms |
コンパイル使用メモリ | 78,532 KB |
実行使用メモリ | 72,896 KB |
最終ジャッジ日時 | 2024-06-27 01:06:01 |
合計ジャッジ時間 | 11,600 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
50,068 KB |
testcase_01 | AC | 55 ms
50,368 KB |
testcase_02 | AC | 82 ms
51,028 KB |
testcase_03 | AC | 199 ms
57,644 KB |
testcase_04 | AC | 445 ms
72,564 KB |
testcase_05 | AC | 444 ms
72,328 KB |
testcase_06 | AC | 447 ms
72,896 KB |
testcase_07 | AC | 434 ms
72,456 KB |
testcase_08 | AC | 448 ms
72,540 KB |
testcase_09 | AC | 460 ms
72,324 KB |
testcase_10 | AC | 428 ms
72,604 KB |
testcase_11 | AC | 443 ms
72,524 KB |
testcase_12 | AC | 439 ms
72,552 KB |
testcase_13 | AC | 413 ms
72,668 KB |
testcase_14 | AC | 428 ms
72,700 KB |
testcase_15 | AC | 463 ms
72,668 KB |
testcase_16 | AC | 444 ms
72,608 KB |
testcase_17 | AC | 442 ms
72,508 KB |
testcase_18 | AC | 452 ms
72,480 KB |
testcase_19 | AC | 445 ms
72,436 KB |
testcase_20 | AC | 53 ms
50,076 KB |
testcase_21 | AC | 55 ms
50,340 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(); } }