結果

問題 No.2563 色ごとのグループ
ユーザー なななななな
提出日時 2023-12-02 19:44:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 1,713 bytes
コンパイル時間 1,993 ms
コンパイル使用メモリ 182,244 KB
実行使用メモリ 29,312 KB
最終ジャッジ日時 2023-12-02 19:44:32
合計ジャッジ時間 7,902 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 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 1 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 6 ms
6,676 KB
testcase_20 AC 19 ms
6,676 KB
testcase_21 AC 12 ms
6,676 KB
testcase_22 AC 9 ms
6,676 KB
testcase_23 AC 15 ms
6,676 KB
testcase_24 AC 163 ms
13,312 KB
testcase_25 AC 157 ms
14,976 KB
testcase_26 AC 247 ms
20,736 KB
testcase_27 AC 300 ms
27,904 KB
testcase_28 AC 282 ms
22,784 KB
testcase_29 AC 397 ms
29,312 KB
testcase_30 AC 425 ms
29,312 KB
testcase_31 AC 399 ms
29,184 KB
testcase_32 AC 406 ms
29,184 KB
testcase_33 AC 241 ms
11,008 KB
testcase_34 AC 232 ms
11,136 KB
testcase_35 AC 231 ms
11,136 KB
testcase_36 AC 234 ms
11,008 KB
testcase_37 AC 234 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
class UnionFind {
public:
  UnionFind() = default;

  // n 個の要素
  explicit UnionFind(size_t n) : m_parents(n), m_sizes(n, 1) {
    std::iota(m_parents.begin(), m_parents.end(), 0);
  }

  // i の root を返す
  int find(int i) {
    if (m_parents[i] == i) {
      return i;
    }

    // 経路圧縮
    return (m_parents[i] = find(m_parents[i]));
  }

  // a の木と b の木を統合
  void merge(int a, int b) {
    a = find(a);
    b = find(b);

    if (a != b) {
      // union by size (小さいほうが子になる)
      if (m_sizes[a] < m_sizes[b]) {
        std::swap(a, b);
      }

      m_sizes[a] += m_sizes[b];
      m_parents[b] = a;
    }
  }

  // a と b が同じ木に属すかを返す
  bool connected(int a, int b) { return (find(a) == find(b)); }

  // i が属するグループの要素数を返す
  int size(int i) { return m_sizes[find(i)]; }

private:
  // m_parents[i] は i の 親,
  // root の場合は自身が親
  std::vector<int> m_parents;

  // グループの要素数 (root 用)
  // i が root のときのみ, m_sizes[i] はそのグループに属する要素数を表す
  std::vector<int> m_sizes;
};
signed main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    if (c[u - 1] == c[v - 1]) {
      uf.merge(u - 1, v - 1);
    }
  }
  map<int, set<int>> mp;
  rep(i, n) {
    int t = uf.find(i);
    mp[c[i]].insert(t);
  }
  int ans = 0;
  for (auto v : mp) {
    int t = v.second.size();
    ans += t - 1;
  }
  cout << ans << endl;
}
0