結果

問題 No.1166 NADA DNA
ユーザー MisterMister
提出日時 2020-08-18 06:50:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 3,000 ms
コード長 1,494 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 92,216 KB
実行使用メモリ 9,672 KB
最終ジャッジ日時 2024-04-20 00:15:56
合計ジャッジ時間 4,422 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 41 ms
5,376 KB
testcase_04 AC 163 ms
9,664 KB
testcase_05 AC 165 ms
9,548 KB
testcase_06 AC 173 ms
9,672 KB
testcase_07 AC 162 ms
9,668 KB
testcase_08 AC 161 ms
9,544 KB
testcase_09 AC 157 ms
9,668 KB
testcase_10 AC 159 ms
9,544 KB
testcase_11 AC 160 ms
9,668 KB
testcase_12 AC 161 ms
9,672 KB
testcase_13 AC 160 ms
9,668 KB
testcase_14 AC 159 ms
9,540 KB
testcase_15 AC 163 ms
9,672 KB
testcase_16 AC 159 ms
9,540 KB
testcase_17 AC 157 ms
9,668 KB
testcase_18 AC 157 ms
9,672 KB
testcase_19 AC 159 ms
9,668 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>

struct UnionFind {
    std::vector<int> par, sz;
    int gnum;

    explicit UnionFind(int n)
        : par(n), sz(n, 1), gnum(n) {
        std::iota(par.begin(), par.end(), 0);
    }

    int find(int v) {
        return (par[v] == v) ? v : (par[v] = find(par[v]));
    }

    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (sz[u] < sz[v]) std::swap(u, v);
        sz[u] += sz[v];
        par[v] = u;
        --gnum;
    }

    bool same(int u, int v) { return find(u) == find(v); }
    bool ispar(int v) { return v == find(v); }
    int size(int v) { return sz[find(v)]; }
};

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::string> ss(n);
    for (auto& s : ss) std::cin >> s;

    std::vector<std::tuple<int, int, int>> es;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            int c = 0;
            for (int k = 0; k < m; ++k) {
                if (ss[i][k] != ss[j][k]) ++c;
            }
            es.emplace_back(c, i, j);
        }
    }
    std::sort(es.begin(), es.end());

    int ans = 0;
    UnionFind uf(n);
    for (auto [c, u, v] : es) {
        if (uf.same(u, v)) continue;
        ans += c;
        uf.unite(u, v);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0