結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2023-04-11 14:54:48 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 504 ms / 3,000 ms |
コード長 | 3,586 bytes |
コンパイル時間 | 2,600 ms |
コンパイル使用メモリ | 91,664 KB |
実行使用メモリ | 62,364 KB |
最終ジャッジ日時 | 2024-10-07 07:49:20 |
合計ジャッジ時間 | 11,704 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
37,236 KB |
testcase_01 | AC | 56 ms
37,104 KB |
testcase_02 | AC | 85 ms
38,776 KB |
testcase_03 | AC | 203 ms
45,848 KB |
testcase_04 | AC | 452 ms
61,864 KB |
testcase_05 | AC | 465 ms
62,120 KB |
testcase_06 | AC | 461 ms
62,364 KB |
testcase_07 | AC | 488 ms
62,272 KB |
testcase_08 | AC | 457 ms
61,880 KB |
testcase_09 | AC | 469 ms
62,100 KB |
testcase_10 | AC | 458 ms
62,028 KB |
testcase_11 | AC | 483 ms
62,180 KB |
testcase_12 | AC | 460 ms
61,844 KB |
testcase_13 | AC | 474 ms
62,000 KB |
testcase_14 | AC | 475 ms
62,060 KB |
testcase_15 | AC | 476 ms
62,332 KB |
testcase_16 | AC | 473 ms
62,200 KB |
testcase_17 | AC | 470 ms
61,984 KB |
testcase_18 | AC | 504 ms
61,984 KB |
testcase_19 | AC | 481 ms
62,132 KB |
testcase_20 | AC | 56 ms
36,748 KB |
testcase_21 | AC | 57 ms
36,784 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(); } }