結果

問題 No.2563 色ごとのグループ
ユーザー takumi152takumi152
提出日時 2023-12-02 15:09:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,543 bytes
コンパイル時間 1,270 ms
コンパイル使用メモリ 116,440 KB
実行使用メモリ 38,640 KB
最終ジャッジ日時 2023-12-02 15:09:08
合計ジャッジ時間 5,222 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 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 2 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 15 ms
6,676 KB
testcase_21 AC 8 ms
6,676 KB
testcase_22 AC 5 ms
6,676 KB
testcase_23 AC 8 ms
6,676 KB
testcase_24 AC 89 ms
17,728 KB
testcase_25 AC 91 ms
19,320 KB
testcase_26 AC 145 ms
26,788 KB
testcase_27 AC 197 ms
35,808 KB
testcase_28 AC 176 ms
30,092 KB
testcase_29 AC 238 ms
38,640 KB
testcase_30 AC 235 ms
38,632 KB
testcase_31 AC 233 ms
38,520 KB
testcase_32 AC 248 ms
38,524 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

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.group[i]);
  }

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

  cout << ans << endl;

  return 0;
}
0