結果

問題 No.1166 NADA DNA
ユーザー chocoruskchocorusk
提出日時 2020-08-12 09:34:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 3,000 ms
コード長 1,584 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 137,376 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-18 00:15:01
合計ジャッジ時間 4,782 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 41 ms
5,376 KB
testcase_04 AC 160 ms
9,304 KB
testcase_05 AC 159 ms
9,164 KB
testcase_06 AC 160 ms
9,216 KB
testcase_07 AC 157 ms
9,344 KB
testcase_08 AC 155 ms
9,136 KB
testcase_09 AC 158 ms
9,216 KB
testcase_10 AC 159 ms
9,344 KB
testcase_11 AC 158 ms
9,344 KB
testcase_12 AC 159 ms
9,296 KB
testcase_13 AC 160 ms
9,344 KB
testcase_14 AC 158 ms
9,216 KB
testcase_15 AC 159 ms
9,200 KB
testcase_16 AC 156 ms
9,164 KB
testcase_17 AC 159 ms
9,224 KB
testcase_18 AC 159 ms
9,164 KB
testcase_19 AC 159 ms
9,344 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int n, l;
string s[1010];
int main()
{
	cin>>n>>l;
	for(int i=0; i<n; i++) cin>>s[i];
	vector<pair<int, P>> v(n*(n-1)/2);
	int c=0;
	for(int i=0; i<n; i++){
		for(int j=0; j<i; j++){
			int w=0;
			for(int k=0; k<l; k++){
				if(s[i][k]!=s[j][k]) w++;
			}
			v[c++]=make_pair(w, P(i, j));
		}
	}
	sort(v.begin(), v.end());
	int ans=0;
	unionfind uf(n);
	for(auto p:v){
		int x=p.second.first, y=p.second.second;
		if(!uf.same(x, y)){
			ans+=p.first;
			uf.unite(x, y);
		}
	}
	cout<<ans<<endl;
	return 0;
}
0