結果

問題 No.2563 色ごとのグループ
ユーザー pmankiraipmankirai
提出日時 2023-12-02 16:00:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 899 ms / 2,000 ms
コード長 4,173 bytes
コンパイル時間 2,728 ms
コンパイル使用メモリ 229,940 KB
実行使用メモリ 51,076 KB
最終ジャッジ日時 2023-12-02 16:00:35
合計ジャッジ時間 11,977 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 5 ms
6,676 KB
testcase_20 AC 20 ms
6,676 KB
testcase_21 AC 10 ms
6,676 KB
testcase_22 AC 7 ms
6,676 KB
testcase_23 AC 11 ms
6,676 KB
testcase_24 AC 145 ms
16,768 KB
testcase_25 AC 166 ms
17,920 KB
testcase_26 AC 234 ms
25,064 KB
testcase_27 AC 292 ms
31,400 KB
testcase_28 AC 272 ms
27,592 KB
testcase_29 AC 399 ms
35,684 KB
testcase_30 AC 390 ms
35,684 KB
testcase_31 AC 434 ms
35,556 KB
testcase_32 AC 409 ms
35,556 KB
testcase_33 AC 891 ms
51,076 KB
testcase_34 AC 836 ms
51,068 KB
testcase_35 AC 849 ms
51,072 KB
testcase_36 AC 899 ms
51,076 KB
testcase_37 AC 882 ms
51,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;

void speedUpIO() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1

#include <algorithm>
#include <cassert>
#include <vector>

// namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
   public:
    dsu() : _n(0) {}
    explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(std::remove_if(result.begin(), result.end(),
                                    [&](const std::vector<int> &v) {
                                        return v.empty();
                                    }),
                     result.end());
        return result;
    }

   private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

// }  // nsamespace atcoder

#endif  // ATCODER_DSU_HPP

/*--------------------------------------------------*/
typedef pair<int, int> P;

const int INF = 1e9;
const ll LINF = 1e18;
const int MX = 100010;

void solve() {
    int n, m;
    cin >> n >> m;
    V<int> c(n);
    rep(i, n) cin >> c[i];
    VV<int> G(n);
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].eb(b);
        G[b].eb(a);
    }
    auto f = [&](set<int> &us) -> int {
        int m = sz(us);
        dsu uf(m);
        map<int, int> id;
        {
            int i = 0;
            for (int u : us) id[u] = i++;
        }
        for (int u : us) {
            for (int v : G[u]) {
                if (us.count(v)) uf.merge(id[u], id[v]);
            }
        }
        return sz(uf.groups()) - 1;
    };
    map<int, set<int>> mp;
    rep(i, n) mp[c[i]].insert(i);
    int ans = 0;
    for (auto [ci, us] : mp) {
        ans += f(us);
    }
    cout << ans << "\n";
}

int main() {
    speedUpIO();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        // cout << solve() << "\n";
        // cout << (solve() ? "YES" : "NO") << "\n";
        // cout << fixed << setprecision(15) << solve() << "\n";
    }
    return 0;
}
0