結果

問題 No.1166 NADA DNA
ユーザー 👑 NachiaNachia
提出日時 2020-09-28 22:29:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 3,000 ms
コード長 900 bytes
コンパイル時間 1,688 ms
コンパイル使用メモリ 174,792 KB
実行使用メモリ 14,748 KB
最終ジャッジ日時 2023-09-15 14:51:29
合計ジャッジ時間 5,195 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 37 ms
6,700 KB
testcase_04 AC 144 ms
13,524 KB
testcase_05 AC 145 ms
13,676 KB
testcase_06 AC 145 ms
13,540 KB
testcase_07 AC 145 ms
13,528 KB
testcase_08 AC 145 ms
13,496 KB
testcase_09 AC 145 ms
14,748 KB
testcase_10 AC 146 ms
14,300 KB
testcase_11 AC 147 ms
14,640 KB
testcase_12 AC 145 ms
13,440 KB
testcase_13 AC 145 ms
13,296 KB
testcase_14 AC 144 ms
14,352 KB
testcase_15 AC 145 ms
13,940 KB
testcase_16 AC 144 ms
13,768 KB
testcase_17 AC 145 ms
13,760 KB
testcase_18 AC 144 ms
14,312 KB
testcase_19 AC 144 ms
13,728 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N, L;
string S[1000];
int D[1000][1000] = {};

struct DSU {
	int N;
	vector<int> V;
	DSU(int n) {
		N = n; V.resize(N);
		rep(i, N) V[i] = i;
	}
	int root(int a) {
		if (V[a] == a) return a;
		return V[a] = root(V[a]);
	}
	void unite(int r, int s) {
		V[root(s)] = root(r);
	}
};

int main() {
	cin >> N >> L;
	rep(i, N) cin >> S[i];
	rep(i, N) rep(j, i) rep(k, L) D[i][j] += ((S[i][k] != S[j][k]) ? 1 : 0);

	priority_queue<pair<int, pair<int, int>>> G;
	rep(i, N) rep(j, i) G.push({ -D[i][j],{i,j} });
	DSU dsu(N);

	int ans = 0;
	while (G.size()) {
		int d = -G.top().first, u = G.top().second.first, v = G.top().second.second;
		G.pop();
		if (dsu.root(u) == dsu.root(v)) continue;
		dsu.unite(u, v);
		ans += d;
	}
	cout << ans << endl;
	return 0;
}
0