結果

問題 No.1166 NADA DNA
ユーザー Nachia
提出日時 2020-09-28 22:29:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 159 ms / 3,000 ms
コード長 900 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 176,972 KB
実行使用メモリ 13,692 KB
最終ジャッジ日時 2024-07-02 17:37:17
合計ジャッジ時間 5,352 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

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