結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2022-08-31 11:24:12 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 474 ms / 3,000 ms |
コード長 | 3,059 bytes |
コンパイル時間 | 2,459 ms |
コンパイル使用メモリ | 79,240 KB |
実行使用メモリ | 74,440 KB |
最終ジャッジ日時 | 2024-04-25 15:28:49 |
合計ジャッジ時間 | 11,432 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
50,512 KB |
testcase_01 | AC | 55 ms
50,580 KB |
testcase_02 | AC | 72 ms
50,892 KB |
testcase_03 | AC | 231 ms
58,952 KB |
testcase_04 | AC | 433 ms
74,040 KB |
testcase_05 | AC | 454 ms
74,260 KB |
testcase_06 | AC | 446 ms
73,956 KB |
testcase_07 | AC | 474 ms
74,108 KB |
testcase_08 | AC | 443 ms
74,072 KB |
testcase_09 | AC | 434 ms
74,212 KB |
testcase_10 | AC | 438 ms
74,104 KB |
testcase_11 | AC | 436 ms
73,896 KB |
testcase_12 | AC | 438 ms
74,440 KB |
testcase_13 | AC | 431 ms
74,116 KB |
testcase_14 | AC | 433 ms
74,324 KB |
testcase_15 | AC | 441 ms
74,108 KB |
testcase_16 | AC | 431 ms
74,200 KB |
testcase_17 | AC | 472 ms
74,228 KB |
testcase_18 | AC | 459 ms
74,288 KB |
testcase_19 | AC | 446 ms
74,172 KB |
testcase_20 | AC | 54 ms
50,532 KB |
testcase_21 | AC | 53 ms
50,448 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[][] inputs = new char[n][]; ArrayList<Path> list = new ArrayList<>(); for (int i = 0; i < n; i++) { inputs[i] = sc.next().toCharArray(); for (int j = 0; j < i; j++) { list.add(new Path(i, j, getDistance(inputs[i], inputs[j]))); } } Collections.sort(list); UnionFindTree uft = new UnionFindTree(n); int ans = 0; for (Path p : list) { if (!uft.same(p)) { ans += p.value; uft.unite(p); } } System.out.println(ans); } static int getDistance(char[] s, char[] t) { int distance = 0; for (int i = 0; i < s.length; i++) { if (s[i] != t[i]) { distance++; } } return distance; } 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; } } 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); } } } 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 String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }