結果
問題 | No.1166 NADA DNA |
ユーザー | tenten |
提出日時 | 2022-08-31 11:24:12 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 529 ms / 3,000 ms |
コード長 | 3,059 bytes |
コンパイル時間 | 3,260 ms |
コンパイル使用メモリ | 79,272 KB |
実行使用メモリ | 63,512 KB |
最終ジャッジ日時 | 2024-11-08 02:31:30 |
合計ジャッジ時間 | 12,348 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
36,764 KB |
testcase_01 | AC | 54 ms
37,248 KB |
testcase_02 | AC | 74 ms
38,004 KB |
testcase_03 | AC | 236 ms
47,312 KB |
testcase_04 | AC | 470 ms
63,112 KB |
testcase_05 | AC | 463 ms
63,360 KB |
testcase_06 | AC | 477 ms
63,296 KB |
testcase_07 | AC | 529 ms
63,512 KB |
testcase_08 | AC | 469 ms
63,128 KB |
testcase_09 | AC | 463 ms
63,252 KB |
testcase_10 | AC | 470 ms
63,256 KB |
testcase_11 | AC | 466 ms
63,312 KB |
testcase_12 | AC | 461 ms
63,332 KB |
testcase_13 | AC | 472 ms
63,260 KB |
testcase_14 | AC | 484 ms
63,124 KB |
testcase_15 | AC | 479 ms
63,328 KB |
testcase_16 | AC | 473 ms
63,128 KB |
testcase_17 | AC | 444 ms
63,112 KB |
testcase_18 | AC | 468 ms
63,108 KB |
testcase_19 | AC | 474 ms
63,312 KB |
testcase_20 | AC | 53 ms
37,188 KB |
testcase_21 | AC | 52 ms
36,948 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(); } }