結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2023-04-11 14:54:48 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 519 ms / 3,000 ms |
コード長 | 3,586 bytes |
コンパイル時間 | 2,717 ms |
コンパイル使用メモリ | 90,824 KB |
実行使用メモリ | 62,244 KB |
最終ジャッジ日時 | 2024-04-16 12:54:39 |
合計ジャッジ時間 | 12,306 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
37,440 KB |
testcase_01 | AC | 52 ms
37,228 KB |
testcase_02 | AC | 79 ms
38,104 KB |
testcase_03 | AC | 206 ms
46,044 KB |
testcase_04 | AC | 509 ms
62,236 KB |
testcase_05 | AC | 482 ms
62,136 KB |
testcase_06 | AC | 509 ms
62,092 KB |
testcase_07 | AC | 500 ms
62,244 KB |
testcase_08 | AC | 492 ms
62,104 KB |
testcase_09 | AC | 498 ms
61,960 KB |
testcase_10 | AC | 492 ms
62,116 KB |
testcase_11 | AC | 518 ms
62,104 KB |
testcase_12 | AC | 502 ms
62,096 KB |
testcase_13 | AC | 480 ms
62,120 KB |
testcase_14 | AC | 499 ms
62,016 KB |
testcase_15 | AC | 519 ms
62,080 KB |
testcase_16 | AC | 503 ms
62,164 KB |
testcase_17 | AC | 449 ms
62,120 KB |
testcase_18 | AC | 501 ms
62,076 KB |
testcase_19 | AC | 493 ms
62,056 KB |
testcase_20 | AC | 53 ms
37,284 KB |
testcase_21 | AC | 52 ms
37,312 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int length = sc.nextInt(); char[][] dna = new char[n][]; PriorityQueue<Bridge> queue = new PriorityQueue<>(); for (int i = 0; i < n; i++) { dna[i] = sc.next().toCharArray(); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int count = 0; for (int k = 0; k < length; k++) { if (dna[i][k] != dna[j][k]) { count++; } } queue.add(new Bridge(i, j, count)); } } long ans = 0; UnionFindTree uft = new UnionFindTree(n); while (queue.size() > 0) { Bridge b = queue.poll(); if (!uft.same(b)) { uft.unite(b); ans += b.value; } } 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(Bridge b) { return same(b.left, b.right); } public void unite(int x, int y) { parents[find(y)] = find(x); } public void unite(Bridge b) { unite(b.left, b.right); } } static class Bridge implements Comparable<Bridge> { int left; int right; int value; public Bridge(int left, int right, int value) { this.left = left; this.right = right; this.value = value; } public int compareTo(Bridge another) { return value - another.value; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); 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 int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }