結果
問題 |
No.1166 NADA DNA
|
ユーザー |
![]() |
提出日時 | 2020-08-12 09:34:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 199 ms / 3,000 ms |
コード長 | 1,584 bytes |
コンパイル時間 | 1,920 ms |
コンパイル使用メモリ | 133,628 KB |
最終ジャッジ日時 | 2025-01-12 21:12:07 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#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; }