結果

問題 No.1166 NADA DNA
ユーザー msm1993msm1993
提出日時 2020-09-11 16:20:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 3,000 ms
コード長 2,119 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 178,948 KB
実行使用メモリ 26,844 KB
最終ジャッジ日時 2023-08-26 23:41:54
合計ジャッジ時間 5,405 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 39 ms
9,060 KB
testcase_04 AC 147 ms
26,708 KB
testcase_05 AC 145 ms
26,716 KB
testcase_06 AC 145 ms
26,776 KB
testcase_07 AC 146 ms
26,708 KB
testcase_08 AC 147 ms
26,700 KB
testcase_09 AC 144 ms
26,700 KB
testcase_10 AC 147 ms
26,648 KB
testcase_11 AC 146 ms
26,452 KB
testcase_12 AC 146 ms
26,712 KB
testcase_13 AC 145 ms
26,792 KB
testcase_14 AC 145 ms
26,728 KB
testcase_15 AC 143 ms
26,660 KB
testcase_16 AC 147 ms
26,712 KB
testcase_17 AC 146 ms
26,840 KB
testcase_18 AC 146 ms
26,720 KB
testcase_19 AC 147 ms
26,844 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
struct UnionFind {  // The range of node number is u 0 v n-1
    vector<int> rank, parents;
    UnionFind() {}
    UnionFind(int n) {  // make n trees.
        rank.resize(n, 0);
        parents.resize(n, 0);
        for (int i = 0; i < n; i++) {
            makeTree(i);
        }
    }
    void makeTree(int x) {
        parents[x] = x;  // the parent of x is x
        rank[x] = 0;
    }
    bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
    void unite(int x, int y) {
        x = findRoot(x);
        y = findRoot(y);
        if (rank[x] > rank[y]) {
            parents[y] = x;
        } else {
            parents[x] = y;
            if (rank[x] == rank[y]) {
                rank[y]++;
            }
        }
    }
    int findRoot(int x) {
        if (x != parents[x]) parents[x] = findRoot(parents[x]);
        return parents[x];
    }
};
 
struct Edge {
    long long u;
    long long v;
    long long cost;
};
bool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } 

struct Kruskal {
    UnionFind uft;
    long long sum;  
    vector<Edge> edges;
    int V;
    Kruskal(const vector<Edge> &edges_, int V_) : edges(edges_), V(V_) { init(); }
    void init() {
        sort(edges.begin(), edges.end(), comp_e);
        uft = UnionFind(V);
        sum = 0;
        for (auto e : edges) {
            if (!uft.isSame(e.u, e.v)) { 
                uft.unite(e.u, e.v);
                sum += e.cost;
            }
        }
    }
};
 
int main() {
    int N, L;
    cin >> N >> L;
    int E=(N * N) / 2;
    vector<string> vec(N);
    for (int i = 0; i < N; i++) {
        cin >> vec[i];
    }
    vector<Edge> edges(E);
    int count = 0;
    for (int i = 0; i < N-1; i++) {
       for (int j = i + 1; j < N; j++) {
         int sum = 0;
         for (int k = 0; k < L; k++) {
           if(vec[i][k] != vec[j][k]) {
             sum++;
           }
         }
         Edge e = {i, j, sum}; 
         edges[count] = e;
         count++;
       }
    }
    Kruskal krs(edges, N);
    cout << krs.sum << endl;
    return 0;
}
0