結果

問題 No.2563 色ごとのグループ
ユーザー takumi152takumi152
提出日時 2023-12-02 17:24:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 1,215 ms
コンパイル使用メモリ 116,952 KB
実行使用メモリ 38,640 KB
最終ジャッジ日時 2023-12-02 17:24:41
合計ジャッジ時間 4,898 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 3 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 4 ms
6,676 KB
testcase_20 AC 13 ms
6,676 KB
testcase_21 AC 7 ms
6,676 KB
testcase_22 AC 5 ms
6,676 KB
testcase_23 AC 8 ms
6,676 KB
testcase_24 AC 85 ms
17,728 KB
testcase_25 AC 83 ms
19,320 KB
testcase_26 AC 140 ms
26,788 KB
testcase_27 AC 175 ms
35,808 KB
testcase_28 AC 161 ms
30,092 KB
testcase_29 AC 215 ms
38,640 KB
testcase_30 AC 220 ms
38,632 KB
testcase_31 AC 210 ms
38,520 KB
testcase_32 AC 211 ms
38,524 KB
testcase_33 AC 82 ms
8,576 KB
testcase_34 AC 80 ms
8,576 KB
testcase_35 AC 81 ms
8,576 KB
testcase_36 AC 82 ms
8,576 KB
testcase_37 AC 81 ms
8,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 998244353;

struct unionfind {
  vector<int> group;
  vector<int> rank;

  unionfind() {
    unionfind(0);
  }

  unionfind(int n) {
    group = vector<int>(n);
    rank = vector<int>(n);
    for (int i = 0; i < n; i++) group[i] = i;
  }

  int root(int x) {
    if (group[x] == x) return x;
    return group[x] = root(group[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rank[rx] > rank[ry]) group[ry] = rx;
    else if (rank[ry] > rank[rx]) group[rx] = ry;
    else if (rx != ry) {
      group[ry] = rx;
      rank[rx]++;
    }
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

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

  int n, m;
  cin >> n >> m;
  vector<int> c(n);
  for (auto &x: c) cin >> x;
  vector<Pii> uv(m);
  for (auto &[u, v]: uv) {
    cin >> u >> v;
    u--;
    v--;
  }

  unionfind uf(n);
  for (auto &[u, v]: uv) {
    if (c[u] == c[v]) {
      uf.unite(u, v);
    }
  }

  unordered_map<int, unordered_set<int>> group;
  for (int i = 0; i < n; i++) {
    group[c[i]].insert(uf.root(i));
  }

  int ans = 0;
  for (auto &[k, v]: group) {
    ans += max(0, (int) v.size() - 1);
  }

  cout << ans << endl;

  return 0;
}
0