結果
| 問題 |
No.1166 NADA DNA
|
| ユーザー |
tenten
|
| 提出日時 | 2021-01-22 08:44:52 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 676 ms / 3,000 ms |
| コード長 | 2,359 bytes |
| コンパイル時間 | 3,006 ms |
| コンパイル使用メモリ | 78,928 KB |
| 実行使用メモリ | 57,768 KB |
| 最終ジャッジ日時 | 2024-12-26 02:50:25 |
| 合計ジャッジ時間 | 16,321 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int length = sc.nextInt();
char[][] all = new char[n][];
for (int i = 0; i < n; i++) {
all[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++) {
queue.add(new Path(i, j, getCost(all[i], all[j])));
}
}
UnionFindTree uft = new UnionFindTree(n);
long cost = 0;
while (queue.size() > 0) {
Path p = queue.poll();
if (!uft.same(p)) {
uft.unite(p);
cost += p.value;
}
}
System.out.println(cost);
}
static int getCost(char[] a1, char[] a2) {
int cost = 0;
for (int i = 0; i < a1.length; i++) {
if (a1[i] != a2[i]) {
cost++;
}
}
return cost;
}
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);
}
}
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;
}
}
}
tenten